我正尝试使用模拟2.0.2和模拟2.0。 以前,我使用powermock模拟一些局部变量: 我在测试课上用过
@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class})
whenNew(MyClass.class).withAnyArguments().thenReturn(myClassMock);
使用Power模拟1.6一切都很好。 当我尝试移至Powermock 2.x时,在PowerMock中找不到 whenNew()方法。 在新的Powermock中这有何变化? 依赖项:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.15.0</version>
<scope>test</scope>
</dependency>
通过Powermock兼容哪个版本的Mockito? -我看到在用嘲讽支持Powermock方面存在一些问题
答案 0 :(得分:1)
我错过了定义旧api组件的部分,请尝试powermock-api-mockito2
。
这是我在我的一个项目中使用的内容(默认为mockito-core 2.28.2
)
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>