Search code examples
javajunitpowermockmockstatic

IllegalStateException complaining PowerMockRule being loaded by powermock classloader rather than system classloader


@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticMethodsHolder.class)

public class MockNTestStaticMethodsHolder {

  @Rule public PowerMockRule rule = new PowerMockRule();

  @Test public void staticSvcClientMethod () {

    // blah blah blah
    mockstatic (StaticMethodsHolder.class);

    expect (StaticMethodsHolder.TomBradyIsStillTheBest()).andReturn(UNQUESTIONABLY);
    expect (StaticMethodsHolder.NEPatriotsStillTheBest()).andReturn(MAYBE);
    expect (StaticMethodsHolder.NEPatriotsLiiWereIdiots()).andReturn(TOTALLY);
    expect (StaticMethodsHolder.NEPatriotsWinsLiii()).andReturn(RU_KIDDING_ME);

  }

}

Maven dependencies/properties in the following order:

  1. powermock-version 1.6.6
  2. easymock-version 3.4
  3. easymock
  4. powermock-module-junit4
  5. powermock-api-easymock
  6. powermock-module-junit4-rule-agent (removing this causes constructor issues)
  7. powermock-module-junit4-rule (removing this dependency has no effect)
  8. powermock-classloading-xstream

Runtime error:

java.lang.IllegalStateException PowerMockRule can only be used
with the system classloader but was loaded by
org.powermock.core.classloader.MockClassLoader.

PowerMock is biting is own tail. It wants to use its own classloader, but the JVM says PowerMockRule must be loaded by system's.

What can I do to resolve this?


Solution

  • You are already using the PowermockRunner, you do not need to use the rule.

    https://github.com/powermock/powermock/wiki/powermockrule

    if you were using a different runner while requiring Powermock functionality that is the use case for the Rule.