Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Verification

verifyNoMoreInteractions and verifyNoInteractions

Catch unexpected calls with verifyNoMoreInteractions and verifyNoInteractions, understand what counts as verified, and learn why overusing them makes tests brittle.

Ordinary verify calls answer the question "did this happen?". Sometimes you also want to know whether anything else happened: did the code send an extra email, call the database when it should not have, or touch a collaborator at all? Mockito offers two methods for that: verifyNoInteractions(mock...) ("this mock was never used") and verifyNoMoreInteractions(mock...) ("every call this mock received has been verified already"). Used well, they close loopholes. Used everywhere, they make tests brittle. This lesson shows both sides.

Real Mockito code is read-only. Runnable examples use the Mini stand-in from What Is Mockito?. One difference worth knowing: with Mockito's strict stubs (the JUnit extension), calls that matched a stubbing count as verified automatically, so you do not have to verify them again. The stand-in does not do this, so its examples verify every call explicitly.

verifyNoInteractions: "never touched"

Java
verifyNoInteractions(mailer);
verifyNoInteractions(mailer, auditLog, metrics);    // several mocks at once

It fails if the mock received any call at all. This is the compact way to assert an early return: the code under test saw invalid input and must not have contacted anything. It replaces the older verifyZeroInteractions, which was deprecated and later removed.

verifyNoMoreInteractions: "everything is accounted for"

Java
verify(mailer).send("[email protected]", "Welcome!");
verifyNoMoreInteractions(mailer);

This fails when the mock received a call that you have not verified. Mockito keeps a "verified" flag on every recorded call. A normal verify(...) marks the matching calls verified, and verifyNoMoreInteractions looks for any call whose flag is still off. Call it last, after all your regular verifications.

Try it: catching a surprise call

The service below has a bug: it sends a second, unwanted email to the admin. The first verification passes, so a test with only verify(mailer).send(user, ...) would stay green. verifyNoMoreInteractions exposes the extra call.

Java Finding an unexpected interaction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

If the admin copy is a requirement, you add verify(mailer).send("[email protected]", ...) and the test stays green. If it is not, the test has found real over-eager behavior.

Try it: "never touched" as an early-return check

Java verifyNoInteractions with several mocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

How "verified" works

Three rules explain every surprise:

  1. A call becomes verified when a verify(...) with matching method and arguments (and an accepted count) examines it.
  2. verifyNoMoreInteractions fails for any call that is still unverified. The stand-in and Mockito behave the same.
  3. In real Mockito with strict stubs (JUnit extension or Mockito.mockitoSession().strictness(STRICT_STUBS)), a call that matched a stubbing is considered verified automatically, because the stub is already proof that you expected it. Without strict stubs you would have to verify those lookups too, which is exactly the noise you want to avoid.

When to use them, and when not

Use Do not use
verifyNoInteractions for early returns and guard clauses verifyNoMoreInteractions at the end of every test
verifyNoMoreInteractions on a collaborator with side effects (mail, payment, audit log) verifyNoMoreInteractions on a repository you only read from
When "nothing else may happen" is a real rule (security, billing) To "be thorough"

Mockito's own documentation warns that verifyNoMoreInteractions is not a recommended routine step: it couples the test to the exact set of calls, so adding a harmless log call or an extra read to the production code breaks the test. Aim it at collaborators where an extra call would be a bug.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does verifyNoInteractions(mailer) check?

    verifyNoInteractions fails as soon as the mock has any recorded call.
  2. 2. Where does verifyNoMoreInteractions belong in a test?

    It looks for calls that have not been verified yet, so all your intended verifications must come first.
  3. 3. Why is verifyNoMoreInteractions on every mock in every test discouraged?

    Over-using it freezes the exact sequence of calls, so unrelated refactorings make the test fail.
  4. 4. With strict stubs in real Mockito, what happens to calls that matched a stubbing?

    A used stub already proves that the call was expected, so verifyNoMoreInteractions does not complain about it.

Summary

  • verifyNoInteractions(mocks...) asserts that the mocks were never used; ideal for early-return paths.
  • verifyNoMoreInteractions(mocks...) asserts that every call has been verified; put it last.
  • A call is verified when a matching verify examines it; with strict stubs, stubbed calls count as verified.
  • Use both on side-effect collaborators where extra calls would be bugs, and avoid them as a routine habit.

Next: Verifying Call Order with InOrder