Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError when using Powermock

I'm running a junit test case using the PowerMock test runner. I'm using the following command line to execute it:

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

When doing so I am receiving this error:

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

How can I fix it?

like image 959
lukas Avatar asked Mar 13 '11 21:03

lukas


2 Answers

I just solved this one now, when I added the @RunWith(PowerMockRunner.class) attribute, eclipse automatically imported:

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

All I needed to do is change it to be:

import org.powermock.modules.junit4.PowerMockRunner; 

And now it works fine with JUnit 4.8.2.

The 2nd runner is for when running with older versions of JUnit - specifically 4.3 and older.

like image 196
RonK Avatar answered Oct 19 '22 10:10

RonK


See 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.

like image 22
juan Avatar answered Oct 19 '22 08:10

juan