2011-03-13 189 views
23

我正在运行使用PowerMock测试运行器的junit测试用例。 我使用下面的命令行来执行它:使用Powermock时NoClassDefFoundError

java -cp .:junit-4.9b2.jar:easymock-3.0.jar:powermock-easymock-1.4.8-full.jar org.junit.runner.JUnitCore SampleTest 

在这样做时,我收到此错误:

initializationError(SampleTest) 
java.lang.NoClassDefFoundError: org/junit/internal/runners/TestClassRunner 
... 

我怎样才能解决呢?

回答

0

我解决了这个问题。我使用旧版本的junit-4.0.jar。但我仍然不明白为什么缺少类TestClassRunner,特别是在包powermock-easymock-junit-1.4.8.zip(有junit-4.8.2.jar)? junit-4.8.2.jar也缺少类TestClassRunner。

+0

这不是正确的解决方案 - 只是一种变通方法。查看我的回答 – RonK 2011-07-05 07:08:56

63

我只是解决了这一个,现在,当我加了@RunWith(PowerMockRunner.class)属性,eclipse自动导入:

import org.powermock.modules.junit4.legacy.PowerMockRunner; 

所有我需要做的就是改变它是:

import org.powermock.modules.junit4.PowerMockRunner; 

而现在使用JUnit 4.8.2可以正常工作。

第二名参赛选手使用老版本的JUnit - 特别是4.3及以上版本。

+0

感谢您的回答。 – jatanp 2012-11-26 08:13:33

+0

非常感谢RonK! – NiranjanBhat 2016-07-15 09:25:20

+1

非常感谢你:) – shafeeq 2016-09-19 09:02:51

5

here

You're probably using the wrong PowerMockRunner. There's one runner made for JUnit 4.4 and above and a second runner made for JUnit 4.0-4.3 (although the latter also works for some older minor versions of JUnit 4.4).
Try switching from the org.powermock.modules.junit4.PowerMockRunner to org.powermock.modules.junit4.legacy.PowerMockRunner or vice versa. Look at the getting started guide to see how to configure this in maven.

相关问题