mock void方法:
Mockito.doNothing().when(类对象).methodName();
Mockito.doThrown(异常).when(类对象).methodName();
// 自定义返回内容
Mockito.doAnswer(new Answer(){...}).when(类对象).methodName();
mock有返回值的方法:
Mockito.when(类实例.方法(arg1, arg2, ...)).thenReturn(...);
Mockito.when(myService2.methodName()).thenReturn(null);
class MyService2 {
Object methodName(){
//...
}
}
更多请参考:https://site.mockito.org