Andreas_D Andreas_D - 1 year ago 157
Java Question

Mockito - how to verify that a mock was never invoked

I'm looking for a way to verify with Mockito, that there wasn't any interaction with a given mock during a test. It's easy to achieve that for a given method with verification mode

, but I haven't found a solution for the complete mock yet.

What I actually want to achieve: verify in tests, that nothing get's printed to the console. The general idea with jUnit goes like that:

private PrintStream systemOut;

public void setUp() {
// spy on System.out
systemOut = spy(System.out);

public void tearDown() {
verify(systemOut, never()); // <-- that doesn't work, just shows the intention

has tons of methods and I really don't want to verify each and every one with separate verify - and the same for

So I hope, if there's an easy solution, that I can, given that I have a good test coverage, force the software engineers (and myself) to remove their (my) debug code like
prior to committing changes.

Answer Source

Use this :

import static org.mockito.Mockito.verifyZeroInteractions;

// ...

private PrintStream backup = System.out;

public void setUp() {

public void tearDown() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download