我遇到PowerMock和Mockito兼容性问题。
我有测试班:
@ActiveProfiles("test")
@PrepareForTest(LanguageUtils.class)
public class MyControllerTest {
@Mock
private MyMockClass MyMockClass;
@Autowired
private MyController myController;
@Rule
public PowerMockRule rule = new PowerMockRule();
}
我的POM看起来像:
<junit-version>4.12</junit-version>
<hamcrest-version>1.3</hamcrest-version>
<mockito-version>1.10.19</mockito-version>
<powermock-version>1.6.4</powermock-version>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${hamcrest-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-version}</version>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>${powermock-version}</version>
<scope>test</scope>
</dependency>
当我尝试运行测试时,我得到了ClassNotFoundException。错误导致PowerMockRule,因为当我删除此行时,错误消失。
完整的堆栈跟踪看起来像:
java.lang.RuntimeException: java.lang.ClassNotFoundException: org.powermock.classloading.DeepCloner
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:68)
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:34)
at org.powermock.classloading.ClassloaderExecutor.createDeepCloner(ClassloaderExecutor.java:106)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:88)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:57)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.powermock.classloading.DeepCloner
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:66)
... 20 more
答案 0 :(得分:6)
这是PowerMock的一个已知问题(请参阅,例如bug 597)。简而言之,如果您使用规则代理模块替换规则模块,它应该可以解决问题。即,替换此要求:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>${powermock-version}</version>
<scope>test</scope>
</dependency>
使用:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>${powermock-version}</version>
<scope>test</scope>
</dependency>