如何用PowerMock模拟静态而不使用PowerMockRunner



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

}

相关内容

  • 没有找到相关文章

最新更新