PowerMockito.verifyStatic()带有参数用法说明

时间:2016-07-08 15:04:30

标签: java mocking mockito powermockito

我正在研究以下使用Range("A1") = "This is a String" 框架的示例:

PowerMockito

我试图找出代码在以下“耦合”行中检查的行为:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Static.class)
public class YourTestCase {
    @Test
    public void testMethodThatCallsStaticMethod() {
        // mock all the static methods in a class called "Static"
        PowerMockito.mockStatic(Static.class);
        // use Mockito to set up your expectation
        Mockito.when(Static.firstStaticMethod(param)).thenReturn(value);
        Mockito.when(Static.secondStaticMethod()).thenReturn(123);

        // execute your test
        classCallStaticMethodObj.execute();

        // Different from Mockito, always use PowerMockito.verifyStatic() first
        // to start verifying behavior
        PowerMockito.verifyStatic(Mockito.times(2));
        // IMPORTANT:  Call the static method you want to verify
        Static.firstStaticMethod(param);


        // IMPORTANT: You need to call verifyStatic() per method verification, 
        // so call verifyStatic() again
        PowerMockito.verifyStatic(); // default times is once
        // Again call the static method which is being verified 
        Static.secondStaticMethod();

        // Again, remember to call verifyStatic()
        PowerMockito.verifyStatic(Mockito.never());
        // And again call the static method. 
        Static.thirdStaticMethod();
    }
}

每对中的确切检查是什么?例如:第一对检查从准备好的类中调用了两个静态方法?

1 个答案:

答案 0 :(得分:0)

PowerMockito.verifyStatic(Mockito.times(2));
Static.firstStaticMethod(param);

表示 Static.firstStaticMethod 被调用了两次。