我正在尝试使用模拟2.0.2和模拟2.0。以前,我使用powermock模拟一些局部变量:我在测试课上用过
@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class})
whenNew(MyClass.class).withAnyArguments().thenReturn(myClassMock);
Power 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>
并且顺便说一下,哪个版本的mockito与Powermock兼容? -我看到在用嘲笑支持Powermock方面存在一些问题
我错过了您定义旧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>