@Runner
不适合@ClassRule
,所以我尝试使用:
@ClassRule
public static PowerMockRule rule = new PowerMockRule();
但是@PrepareForTest
什么也没做。
我的代码看起来像这样:
@PrepareForTest({SomeClass.class})
public class DynamicVipTest {
@ClassRule
public static SomeClassRule someClassRule = new SomeClassRule();
@ClassRule
public static PowerMockRule rule = new PowerMockRule();
@BeforeClass
public static void setupClass() {
PowerMock.mockStatic(SomeClass.class);
expect(SomeClass.someMethod().andReturn("someValue").anyTimes();
}
}
expect
最终调用SomeClass.someMethod
而不是创建期望。
模拟静态的最新最好的方法是什么?
希望有比以下更好的"解决方案":
@RunWith(PowerMockRunner.class)
@PrepareForTest({SomeClass.class})
public class DynamicVipTest {
public static SomeClassRule someClassRule = new SomeClassRule();
@ClassRule
public static PowerMockRule rule = new PowerMockRule();
@BeforeClass
public static void setUpClass() {
someClassRule.before();
PowerMock.mockStatic(SomeClass.class);
expect(SomeClass.someMethod().andReturn("someValue").anyTimes();
}
@AfterClass
public static void tearDownClass() {
someClassRule.after();
}
}