Mockito - how to verify that a mock was never invoked
Use this :
import static org.mockito.Mockito.verifyZeroInteractions;
// ...
private PrintStream backup = System.out;
@Before
public void setUp() {
System.setOut(mock(PrintStream.class));
}
@After
public void tearDown() {
verifyZeroInteractions(System.out);
System.setOut(backup);
}
Since the original correct answer, verifyZeroInteractions
has been deprecated, use verifyNoInteractions
instead:
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
public class SOExample {
@Test
public void test() {
Object mock = mock(Object.class);
verifyNoInteractions(mock);
}
}
verifyZeroInteractions(systemOut);
As noted in comments, this doesn't work with a spy.
For a roughly equivalent but more complete answer, see the answer by gontard to this question.