我有这个(简化的)服务类:
public interface EventListener {
void call();
}
public class MyService {
private final EventListener eventListener;
private final List<String> elements = new LinkedList<>();
public MyService(EventListener eventListener) {
this.eventListener = eventListener;
}
public void addElement(String element) {
elements.add(element);
eventListener.call();
}
public void removeElement(String element) {
elements.remove(element);
eventListener.call();
}
}
而这种测试方法:
@Test
public void remove_should_call_event_listener() {
// arrange
EventListener eventListener = Mockito.mock(EventListener.class);
MyService myService = new MyService(eventListener);
myService.addElement("dummy");
// act
myService.removeElement("dummy");
// assert
Mockito.verify(eventListener).call();
}
如何在“安排”期间告诉Mockito忽略eventListener.call()
的调用并在“act”期间仅验证调用?我想验证在eventListener.call()
期间调用myService.removeElement(...)
并忽略所有其他调用。
在演戏前,请致电:
Mockito.reset(eventListener); // resets the set-up also
要么
Mockito.clearInvocations(eventListener) // resets only the invocation history