IllegalStateException抱怨PowerMockRule由powermock类加载器而不是系统类加载器加载

时间:2018-02-22 20:12:53

标签: java junit powermock mockstatic

@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依赖项/属性:

  1. powermock-version 1.6.6
  2. easymock-version 3.4
  3. EasyMock的
  4. powermock模块-junit4
  5. powermock-API-EasyMock的
  6. powermock模块-junit4规则代理 (删除它会导致构造函数问题)
  7. powermock模块-junit4规则 (删除此依赖项无效)
  8. powermock-类加载-XStream的
  9. 运行时错误:

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

    PowerMock正在咬自己的尾巴。它想要使用自己的类加载器,但JVM说PowerMockRule必须由系统加载。

    我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您已经在使用PowermockRunner,您无需使用该规则。

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

如果您在使用Powermock功能时使用不同的跑步者,而Powermock功能是规则的用例。