@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依赖项/属性:
运行时错误:
java.lang.IllegalStateException PowerMockRule can only be used
with the system classloader but was loaded by
org.powermock.core.classloader.MockClassLoader.
PowerMock正在咬自己的尾巴。它想要使用自己的类加载器,但JVM说PowerMockRule必须由系统加载。
我该怎么做才能解决这个问题?
答案 0 :(得分:1)
您已经在使用PowermockRunner,您无需使用该规则。
https://github.com/powermock/powermock/wiki/powermockrule
如果您在使用Powermock功能时使用不同的跑步者,而Powermock功能是规则的用例。