1

我正在使用 Robolectric 2.1、Powermock 1.5.1,我想简单地编写一个静态方法测试。我只是想验证一个类的静态方法在传入 false 作为参数时永远不会被调用:

@RunWith(RobolectricTestRunner.class)
@PrepareForTest(MyStaticClassToTest.class)
public class TestClass {
    @Test
    public void staticMethod_shouldNeverBeCalled() throws Exception {
        MyStaticClassToTest instance = new MyStaticClassToTest();

        PowerMockito.mockStatic(MyStaticClassToTest.class);
        PowerMockito.when(MyStaticClassToTest.staticMethod(new Context)).thenReturn("wtf?");

        instance.iCallStaticMethod(false);

        PowerMockito.verifyStatic(times(1)); //i want this to fail, but it keeps passing
        MyStaticClassToTest.staticMethod(context);
    }
}

我有一个定义如下的类:

public class MyStaticClassToTest {
    public void iCallStaticMethod(boolean yesOrNo){
        if(yesOrNo) staticMethod(new Context());
    }
    public static String staticMethod(Context androidContext){
        //do something... 
        return null;
    }
}

为什么我的测试一直通过?我希望它验证静态方法永远不会被调用。

4

0 回答 0