41

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?

RonK
  • 9,472
  • 8
  • 51
  • 87
lukas
  • 421
  • 1
  • 4
  • 4

5 Answers5

88

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.

RonK
  • 9,472
  • 8
  • 51
  • 87
  • 1
    Thank 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** and `powermock-module-junit` for **junit4.4 or newer**. Check [this pom configuration](https://github.com/powermock/powermock/wiki/Mockito-2-Maven#junit-40-43) – emrekgn May 21 '18 at 13:01
7

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;
Roman Skydan
  • 5,478
  • 4
  • 19
  • 39
  • Although it is mentioned in the power mock maven setup that 4.0-4.3 junit should use powermock-module-junit4-legagcy artifact, but the above error occurs. – Himanshu Shukla Sep 08 '22 at 17:58
7

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.

juan
  • 80,295
  • 52
  • 162
  • 195
1

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.

lukas
  • 421
  • 1
  • 4
  • 4
0

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>
Keerthikanth Chowdary
  • 728
  • 1
  • 10
  • 17