如何在不使用PowerMockRunner的情况下使用PowerMock模拟静态?

时间:2014-05-06 23:44:22

标签: powermock

@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而不是创造期望。

模拟静力学的最新最佳方法是什么?

1 个答案:

答案 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();
    }

}