我有一个我嘲笑的void
方法的服务。
我想登录或system.out.println("..")
,当无效方法被击中时。
public class MyService {
public void call(){
// do something.
}
}
private MyService mySerivce = mock(MyService.class);
我想从调用方法主体中记录一条消息,我该如何嘲笑?就像我可以将答案与非void方法一起使用。
verify(myService, times(1)).call();
myService.call();
verify(my)
答案 0 :(得分:1)
运行测试后,使用spy
并验证其状态(老实说-您也可以使用mock
进行操作,但是如果需要,可以使用spy
来测试真实对象):
//Given
MyService service = new MyService();
MyService serviceSpy = Mockito.spy(service);
//When
serviceSpy.call();
//Then
long count = Mockito.mockingDetails(serviceSpy)
.getInvocations()
.stream()
.filter(i -> i.toString().contains("service.call()"))
.count();
if (count > 0)
System.out.println("Method was called " + count + " times.");
else
System.out.println("Method wasn't called at all.");
//TODO: of course this isn't correct test because it hasn't any assertions.