https://github.com/powermock/powermock/wiki/Mockito
官方文档
mock static
static 类
模拟方法调用:
@PrepareForTest
添加类
@PrepareForTest(Static.class) 包含static方法的类
- 调用PowerMockito.mockStatic() mock一个static class
PowerMockito.mockStatic(Static.class); //该static class中的方法均不会真实执行
PowerMockito.mockStatic(Static.class, CALLS_REAL_METHODS); //该static class中的方法均会真实执行
- 模拟方法返回
Mockito.when(Static.firstStaticMethod(param)).thenReturn(value);
验证函数的行为
注意:调用PowerMockito.verifyStatic(Static.class)
的前后都要调用static方法,verifyStatic之前调用static方法是真实调用,之后调用static方法仅为了告知verifystatic方法是哪个static方法调用时需要验证
- 验证static方法(指定参数)被调用
PowerMockito.mockStatic(Static.class);
// Test
Static.firstStaticMethod(param); // 1调用static方法是真实调用
// Validation
PowerMockito.verifyStatic(Static.class); // 2验证函数行为
Static.firstStaticMethod(param); // 3告知verifystatic方法是哪个static方法调用时需要验证
- 验证static方法(任意入参)被调用
PowerMockito.mockStatic(Static.class);
// Test
Static.thirdStaticMethod(Mockito.anyInt());
// Validation
PowerMockito.verifyStatic(Static.class);
Static.thirdStaticMethod(Mockito.anyInt());
- 验证static(public)方法调用次数
PowerMockito.mockStatic(Static.class);
// Test
Static.staticMethod();
Static.staticMethod();
Static.staticMethod();
// Validation
PowerMockito.verifyStatic(Static.class, Mockito.times(3));
Static.staticMethod();
- 验证static(private)方法调用次数
PowerMockito.mockStatic(Static.class, CALLS_REAL_METHODS);
// Test
Static.staticMethod(); // 调用private static 方法 privateStaticMethod()
// Validation
PowerMockito.verifyPrivate(Static.class, Mockito.times(1)).invoke("privateStaticMethod");
static方法抛异常
- static方法(public)抛异常
执行后StaticService.executeMethod();之后会抛出异常ArrayStoreException
PowerMockito.mockStatic(Static.class);
PowerMockito.doThrow(new ArrayStoreException("Mock error")).when(StaticService.class, "executeMethod");
StaticService.executeMethod();
- static方法(private)抛异常
PowerMockito.mockStatic(Static.class, CALLS_REAL_METHODS);
PowerMockito.doThrow(new ArrayStoreException("Mock error")).when(StaticService.class, "privateMethod");
StaticService.executeMethod(); //executeMethod中调用privateMethod
部分mock
使用spy,真实执行实例或static类
- spy实例
List list = new LinkedList();
List spy = spy(list);
//Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty)
when(spy.get(0)).thenReturn("foo");
//You have to use doReturn() for stubbing
doReturn("foo").when(spy).get(0);
- spy static类
PowerMockito.spy(Static.class); //该static class中的方法均会真实执行
- 验证public函数行为
方法带入参时,可填写真实参数,也可通过Mockito获取随机值
MyClassObj obj = PowerMockito.spy(new MyClassObj());
obj.methodToMock(); // test
Mockito.verify(mockObj, times(1)).methodToMock(); // Validation验证方法调用次数
- 验证private函数行为
MyClassObj obj = PowerMockito.spy(new MyClassObj());
obj.methodToMock(); // test 调用private方法
PowerMockito.verifyPrivate(mockObj, times(1)).invoke("privateStaticMethod"); // Validation验证方法调用次数
- 模拟新对象的创建
注意:创建新对象的类必须在prepare中,eg:X类中执行了new MyClass(),则需要@PrepareForTest(X.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest(X.class)
public class XTest {
@Test
public void test() {
whenNew(MyClass.class).withNoArguments().thenThrow(new IOException("error message"));
X x = new X();
x.y(); // y is the method doing "new MyClass()"
..
}
}
- 验证模拟新对象的创建
PowerMockito.verifyNew(MyClass.class).withNoArguments();
注意:验证verify时,static方法使用verifyStatic、非static的public方法使用verify、非static的private方法使用verifyPrivate
- mock final
MyFinalMock myFinalMock = PowerMockito.spy(new MyFinalMock ());
PowerMockito.doThrow(new ArrayStoreException("Mock error")).when(myFinalMock).myFinalMethod();
myFinalMock.myFinalMethod();
- mock private
Tested tested= PowerMockito.spy(new Tested());
// test
tested.execute(); // 执行private methodToExpect方法
when(tested, "methodToExpect", argument).thenReturn(myReturnValue);
A full example of spying
public方法的mock与verify
@RunWith(PowerMockRunner.class)
// We prepare PartialMockClass for test because it's final or we need to mock private or static methods
@PrepareForTest(PartialMockClass.class)
public class YourTestCase {
@Test
public void spyingWithPowerMock() {
PartialMockClass classUnderTest = PowerMockito.spy(new PartialMockClass());
// use Mockito to set up your expectation
Mockito.when(classUnderTest.methodToMock()).thenReturn(value);
// execute your test
classUnderTest.execute();
// Use Mockito.verify() to verify result
Mockito.verify(mockObj, times(2)).methodToMock();
}
}
A full example of partial mocking of a private method
@RunWith(PowerMockRunner.class)
// We prepare PartialMockClass for test because it's final or we need to mock private or static methods
@PrepareForTest(PartialMockClass.class)
public class YourTestCase {
@Test
public void privatePartialMockingWithPowerMock() {
PartialMockClass classUnderTest = PowerMockito.spy(new PartialMockClass());
// use PowerMockito to set up your expectation
PowerMockito.doReturn(value).when(classUnderTest, "methodToMock", "parameter1");
// execute your test
classUnderTest.execute();
// Use PowerMockito.verify() to verify result
PowerMockito.verifyPrivate(classUnderTest, times(2)).invoke("methodToMock", "parameter1");
}
}
全部mock
使用mock创建对象,所有方法均不真实执行
Powermockito.mock(MockClass.class)