我正在研究以下使用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();
}
}
我试图弄清楚代码在以下"耦合"行中检查的行为类型:
............
PowerMockito.verifyStatic(Mockito.times(2));
Static.firstStaticMethod(param);
..................
PowerMockito.verifyStatic(); // default times is once
Static.secondStaticMethod();
..................
PowerMockito.verifyStatic(Mockito.never());
Static.thirdStaticMethod();
每对中的确切检查是什么? 例如:第一对检查是否从准备好的类中调用了两个静态方法?
PowerMockito.verifyStatic(Mockito.times(2));
Static.firstStaticMethod(param);
表示 Static.firstStaticMethod 被调用了两次。