@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 依赖项/属性按以下顺序排列:
- powermock 版本 1.6.6
- easymock 3.4 版
- 简易模拟
- powermock-module-junit4
- powermock-api-easymock
- powermock-module-junit4-rule-agent(删除它会导致构造函数问题)
- powermock-module-junit4-rule(删除这个依赖没有效果)
- 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 必须由系统加载。
我能做些什么来解决这个问题?