DEVLOPN Audit

verify(mock.method()) — the green test that let a double call slip through

In Mockito, verify without an explicit .called(1) can stay green while the method fires twice. Why precision in tests protects the reader — and yourself.

For a long time I wrote verify(mock.method()) thinking .called(1) was the default.

Then Michael, in review: "what exactly are you verifying there?"

Big moment of doubt.

Verify or called(1)? Your test passes to green, does it really verify what you think? Be specific, define called(1)

The bug under the radar

verify(mock.method()) stayed green while the method fired twice. The bug slipped under the radar.

Without an explicit .called(1), the test only checks that the call happened at least once — not exactly once. A double call (double network request, double write) sails right through.

The lesson

Code is read ten times more than it's written. Be precise for the reader, and for yourself.

// Ambiguous: green even if the method fires twice
verify(mock.method());

// Explicit: fails if the call isn't unique
verify(mock.method()).called(1);

And you — do you write .called(1) explicitly or not?