@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模块junit4
powermock-api-easymock
powermock-module-junit4-rule-agent
(删除此将导致构造函数问题)
powermock模块junit4规则
(删除此依赖关系无效)
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必须由系统加载。
我该怎么解决?
最佳答案
您已经在使用PowermockRunner,不需要使用规则。
https://github.com/powermock/powermock/wiki/powermockrule
如果您在需要Powermock功能(该规则的用例)时使用了其他运行程序。