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开发者_JAVA技巧.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?
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.
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.
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;
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.
I am using JUnit 4.0 - 4.3 and I updated my maven dependency to use powermockito 2.0.0-beta.5 version. It just started working.
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
精彩评论