0
@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 版本 1.6.6
  2. easymock 3.4 版
  3. 简易模拟
  4. powermock-module-junit4
  5. powermock-api-easymock
  6. powermock-module-junit4-rule-agent(删除它会导致构造函数问题)
  7. powermock-module-junit4-rule(删除这个依赖没有效果)
  8. powermock-classloading-xstream

运行时错误:

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

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

我能做些什么来解决这个问题?

4

1 回答 1

3

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

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

如果您在需要 Powermock 功能的同时使用不同的运行器,这是该规则的用例。

于 2018-02-22T20:22:35.737 回答