Quick revision
The short answer
A good standard says: verify a call only when the call is the observable effect of the code, such as sending a message, charging a card or publishing an event. Do not verify calls whose results the test already checks through a return value or state, avoid verifyNoMoreInteractions unless 'no other calls' is a real requirement, and prefer captors with clear assertions over complex matchers. The standard should show short good and bad examples, because people copy examples, not rules.
What to remember
In this answer, we wrote a verify() standard around one sentence: tests should fail when behaviour changes and pass when only the implementation changes. That led to four rules: verify observable effects such as messages and payments, do not verify calls whose results are already asserted, use verifyNoMoreInteractions only for real 'no other calls' requirements, and prefer captors with clear assertions over complex matchers. We backed each rule with good and bad examples and rolled it out through templates, review and measurement.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A good standard says: verify a call only when the call is the observable effect of the code, such as sending a message, charging a card or publishing an event. Do not verify calls whose results the test already checks through a return value or state, avoid verifyNoMoreInteractions unless 'no other calls' is a real requirement, and prefer captors with clear assertions over complex matchers. The standard should show short good and bad examples, because people copy examples, not rules.
Why this question is asked
In a large organisation, test style spreads by copying. If the first tests in a code base verify every call, the next thousand tests will too, and then every refactor breaks hundreds of them. A written standard is how a senior engineer stops that pattern without reviewing every pull request personally. Interviewers ask this design question to see whether we can turn experience into rules that are short, justified and easy to apply. In this answer, we will start from the problem the standard solves, then write it rule by rule with the reasoning behind each, include the examples it should carry, and finish with how to roll it out.
1The Problem the Standard Solves
verify() asserts that a mock received a call. Used well, it checks things nothing else can check: that an email went out, that a payment was charged exactly once. Used everywhere, it turns tests into a copy of the implementation. A test that verifies repo.findById(), mapper.toDto(), cache.get() and logger.info() fails the moment someone loads the data differently, even if the result is identical.
So the goal of the standard is simple to state: a test should fail when behaviour changes and keep passing when only the implementation changes. Everything in it follows from that sentence, and we put that sentence at the top so people can reason about cases the rules do not cover.
2Rule 1: Verify Observable Effects Only
The first rule says what to verify. A call deserves verification when it is an effect that the outside world can see and that the code exists to produce: sending an email, publishing a Kafka event, charging a card, writing an audit record that compliance relies on. These calls usually return nothing useful, so verify() is the only way to check them.
The rule also says how to verify them: with the count and arguments that matter to the business. Charging a card must happen exactly once, so times(1) is meaningful there. For a log line, it is not.
Notice that this test would survive almost any refactor of checkout() that keeps the behaviour, because it only checks what a customer or another system would notice.
3Rule 2: Do Not Verify What the Result Already Proves
The second rule says what not to verify. If a method returns a value or changes state that the test asserts, then the calls that produced it are already proven. Verifying them adds no protection, only coupling.
The reasoning goes into the standard next to the example: the stub already makes the test depend on findById(), and the assertion already proves it was used. A second check of the same fact costs us a test failure on every refactor and catches nothing new.
4Rule 3: No verifyNoMoreInteractions by Default
The third rule addresses the most damaging habit. verifyNoMoreInteractions() fails if the mock received any call the test did not verify, so a harmless new call, such as a metrics counter or a cache lookup, turns the test red. The standard allows it, or the clearer verifyNoInteractions(), only when the absence of calls is itself a requirement, and asks for a comment saying which requirement.
Writing the reason as a comment has a useful side effect: if the author cannot name the requirement, they usually realise they do not need the check.
5Rule 4: Captors With Clear Assertions Over Complex Matchers
The fourth rule is about failure messages, because a test is read most carefully when it fails. When we need to check an argument with several fields, a long argThat() lambda fails with 'arguments are different' and a dump of the object. A captor followed by AssertJ assertions says which field was wrong.
| Situation | Rule | Why |
|---|---|---|
| Call is an external effect | verify, with business counts | nothing else can prove it happened |
| Call produced an asserted result | do not verify | the assertion already proves it |
| 'No other calls' is a requirement | verifyNoInteractions, with a comment | strictness only where it is a rule |
| Argument with several fields | captor and AssertJ | failure names the wrong field |
| Argument is a predictable value object | verify with equals() | shortest and clearest |
The standard should also say that plain equality is best of all when the argument is a value object with a correct equals(), such as a record whose fields are all predictable.
6Rolling the Standard Out
A standard that lives only on a wiki page changes little. We keep it to one page, with the table above and three pairs of good and bad tests taken from our own code base, anonymised, because people recognise their own patterns. We link it from the pull request template and from the test templates that generators or archetypes create.
Then we let reviewers cite it by rule number, which keeps review comments short and impersonal. We do not rewrite old tests in a big bang; we apply the standard when a test is touched for another reason. After a few months we can check whether it works by counting how many tests a typical refactor breaks, and we revisit the rules once a year or when the tools change.
- Write one page with examplesrules plus good and bad tests from our code
- Link from PR and test templatespeople meet it where they work
- Reviewers cite rule numbersshort, impersonal comments
- Apply when tests are touchedno big-bang rewrite
- Measure tests broken per refactorreview the standard yearly
5 steps in order. Play them, or step through them one at a time.
7Follow-up questions
Should the standard ban verify() on repositories?
No. Saving an entity can be the observable effect, for example in a command handler. But for repository behaviour, a fake repository or a @DataJpaTest slice with a real database usually gives a stronger test than verifying save().
How do you enforce it without a linter?
Mostly through examples and review. Some rules can be automated, such as flagging verifyNoMoreInteractions without a comment, using a simple static check or an ArchUnit-style rule on test code.
What about verifying calls in legacy tests?
We leave them until the test is changed for another reason, then apply the standard. Rewriting thousands of working tests at once costs a lot and risks removing checks that matter.
8Conclusion
In this answer, we wrote a verify() standard around one sentence: tests should fail when behaviour changes and pass when only the implementation changes. That led to four rules: verify observable effects such as messages and payments, do not verify calls whose results are already asserted, use verifyNoMoreInteractions only for real 'no other calls' requirements, and prefer captors with clear assertions over complex matchers. We backed each rule with good and bad examples and rolled it out through templates, review and measurement.
Open with
I would start the standard with one principle: a test should fail when behaviour changes and keep passing when only the implementation changes. Then four rules. First, verify a call only when it is the observable effect of the code, like sending an email, publishing an event or charging a card, and verify the count that matters to the business, like charging exactly once.
Then, if they want more
Second, do not verify calls whose results the test already checks through a return value or state. If I assert the user's name, verifying findById adds nothing and breaks when loading changes. Third, no verifyNoMoreInteractions by default; it breaks tests on any harmless new call.
It is allowed, or verifyNoInteractions, only when the absence of calls is a requirement, such as a dry run never touching the gateway, with a comment naming that requirement.
Fourth, for arguments with several fields, use a captor with AssertJ instead of a long argThat lambda, because the failure names the wrong field, and use plain equality when the value object supports it.
I would keep it to one page with good and bad examples from our own code, link it from PR templates, let reviewers cite rule numbers, and measure how many tests a refactor breaks.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a test standard for your organisation on when to use verify() in Mockito, so that...": https://scrivio-interview-library.netlify.app/java/mocking/98829ab872/