What are VerificationModes in Mockito (times, never, atLeastOnce, atLeast, atMost) and when would you use each?
answer
- default verify = times(1)
- never() == times(0)
- atLeastOnce / atLeast(n) = lower bound
- atMost(n) = upper bound, catches extras
- mode is verify(mock, MODE)
basics
~10 sA VerificationMode says how many times a call should have happened. times(n) = exactly n, never() = zero, atLeastOnce() = one or more, atLeast(n)/atMost(n) = bounds. Default verify(mock) means times(1).
solid answer
~40 sA VerificationMode is the second argument to verify that constrains how many times an interaction occurred. The default, verify(mock).foo(), means times(1) — exactly once. times(n) asserts an exact count; never() is times(0) and asserts the call never happened. atLeastOnce() and atLeast(n) assert a lower bound (one-or-more, or n-or-more) which is useful when the exact count is non-deterministic but presence matters. atMost(n) asserts an upper bound — handy to catch accidental extra calls, e.g. atMost(1) to prove no duplicate save. You pass the mode as verify(mock, times(2)).foo() or verify(mock, never()).foo(). Choose exact counts when the contract is precise (idempotent save once), and bounded/at-least modes when the count can legitimately vary but you still want to assert a floor or ceiling. Pair never() with verifyNoMoreInteractions for tighter no-call guarantees.
go deeper
Knows times(n) and never() and that bare verify means once.
Can pick among times/never/atLeast/atMost for a given contract and knows counts are per argument match.
Reasons about non-deterministic counts (atLeastOnce) and duplicate-guarding (atMost), and combines never() with verifyNoMoreInteractions for tight guarantees.
Sets suite-wide conventions on when exact counts are worth their brittleness vs bounded modes, and recognizes when count-based assertions signal a design smell.
## What a VerificationMode is `verify` takes an optional second argument — a **VerificationMode** — that says *how many times* the interaction must have occurred. Without it, `verify(mock).foo()` defaults to **exactly once**. You supply a mode like this: ```java verify(mock, times(2)).foo(); // foo() called exactly twice verify(mock, never()).bar(); // bar() never called ``` The modes come from `org.mockito.Mockito` (static imports). ## The modes - **`times(n)`** — the call happened **exactly n** times. `times(1)` is the implicit default. `times(0)` is equivalent to `never()`. - **`never()`** — the call happened **zero** times. Reads more clearly than `times(0)` and signals intent: 'this must NOT happen'. - **`atLeastOnce()`** — **one or more** times. Use when the exact count is irrelevant or non-deterministic but the call must occur at least once. - **`atLeast(n)`** — **n or more** times (lower bound). - **`atMost(n)`** — **at most n** times (upper bound). Great for catching *accidental extra* calls — e.g. `atMost(1)` proves there was no duplicate. - **`calls(n)`** — exactly n, but **only meaningful with InOrder** (it does not 'consume' all matching calls the way times does in ordered verification). ## How to choose | Situation | Mode | |---|---| | Precise contract (save exactly once) | `times(1)` / default | | Must not happen (no email on validation failure) | `never()` | | Must happen, count varies | `atLeastOnce()` / `atLeast(n)` | | Guard against duplicates / runaway loops | `atMost(n)` | ## Counting semantics The count is about a **specific argument match**. `verify(mock, times(2)).foo("a")` requires two calls *with argument "a"*; calls with other arguments don't count. With argument matchers, `verify(mock, times(3)).foo(any())` counts any-argument calls. ## A failure example ```java verify(repo, times(1)).save(user); // If save was called twice, Mockito fails with: // 'Wanted 1 time but was 2 times' ``` ## Pairing with no-more-interactions `never()` only asserts about the *one* method/argument you named. To assert nothing *else* happened on the mock, combine with `verifyNoMoreInteractions(mock)` after your expected verifies, or `verifyNoInteractions(mock)` if you expect the mock untouched entirely.
- What is the difference between never() and verifyNoInteractions?never() asserts one specific method (with given args) was not called, while other interactions may have occurred. verifyNoInteractions(mock) asserts the mock was never touched at all.
- When would atMost be more useful than times?When extra calls are the bug you fear — e.g. atMost(1) proves no accidental duplicate save without pinning the exact count, useful when zero calls is also acceptable in some paths.
saying these in an interview costs you the question
- Assuming verify with no mode allows any number of calls (it means exactly once)
- Using times(0) when never() is clearer, or thinking never() guards the whole mock (it only guards that one call)
- Forgetting that counts are per argument-match, not total calls to the method