Search code examples
javanoclassdeffounderrorpowermock

java.lang.NoClassDefFoundError: Could not initialize class org.powermock.modules.junit4.rule.PowerMockRule


I'm trying to run tests in a legacy Java project and I get this error:

java.lang.NoClassDefFoundError: Could not initialize class org.powermock.modules.junit4.rule.PowerMockRule

The code that is causing the issue is like that:

@Rule
public PowerMockRule rule = new PowerMockRule();

And the PowerMock version is 1.5.6

 <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4</artifactId>
      <version>1.5.6</version>
      <scope>test</scope>
 </dependency>

There is not change in the error even when updating to the latest version of PowerMock - version 2.0.5


Solution

  • add this dependency to get rid of NoClassDefFoundError

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4-rule</artifactId>
      <version>2.0.5</version>
      <scope>test</scope>
    </dependency>
    

    powermock-module-junit4-1.5.6.jar don't have PowerMockRule class file. so use powermock-module-junit4-rule-2.0.5.jar

    refer this documentation

    I hope this helps.