@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
而不是创造期望。
模拟静力学的最新最佳方法是什么?
答案 0 :(得分:0)
希望有一个更好的解决方案'比以下:
@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();
}
}