NoClassDefFoundError when using Powermock
Solution 1
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.
Solution 2
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.
Solution 3
This Exception occurs when you Import the legacy version of PowerMockRunner.class when using JUnit 4.X or higher version since this legacy class is not available to run when using it with @RunWith annotation. I have solved this issue by replacing older legacy version import with the new version.
Incorrect Import:
import org.powermock.modules.junit4.legacy.PowerMockRunner;
Correct Import:
import org.powermock.modules.junit4.PowerMockRunner;
Solution 4
I solved the problem. I used old version junit-4.0.jar. But I still don't understand why is missing the class TestClassRunner especially in the package powermock-easymock-junit-1.4.8.zip (there is junit-4.8.2.jar)? The junit-4.8.2.jar is missing the class TestClassRunner also.
Admin
Updated on December 04, 2020Comments
-
Admin over 3 years
I'm running a
junit
test case using thePowerMock
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?
-
RonK almost 13 yearsThis is not the correct solution - merely a workaround. See my answer
-
emrekgn almost 6 yearsThank you very much for the answer. For anyone using maven, this may also be caused by using wrong powermockito-junit dependency. Make sure that
powermock-module-junit4-legacy
is included for junit4.0-4.3 andpowermock-module-junit
for junit4.4 or newer. Check this pom configuration