Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What do verify(mock, times(2)), never(), atLeastOnce() and inOrder check in Mockito? When should you avoid verifyNoMoreInteractions?

verify(mock, times(2)) checks that a method was called exactly twice, never() that it was not called, and atLeastOnce() that it was called one or more times. inOrder(a, b) checks that calls happened in a given order, even across different mocks. verifyNoMoreInteractions() fails if the mock received any call we did not verify, so we should avoid it unless the absence of other calls is a real requirement, because otherwise harmless new calls break the test.

Why this question is asked

Verification is how a Mockito test checks the effects of code that talks to collaborators: an email was sent, a payment was charged once and not twice, an audit entry was written after the update. Interviewers ask about these modes because they are easy to learn and easy to overuse, and over-verification is one of the main reasons test suites break on every refactor. In this answer, we will recap what verify() does, go through each counting mode with an example, look at checking order, and then see when verifyNoMoreInteractions helps and when it hurts.

1A Quick Recap of verify()

A Mockito mock records every call made to it: the method and the arguments. verify(mock).method(args) looks through those recorded calls after the code under test has run and checks that a matching call exists. With no mode, verify() means times(1), exactly once. If the check fails, Mockito throws an error that lists the calls it did record, which usually makes the cause easy to see.

So verify() is an assertion about interactions, while assertEquals() is an assertion about results. We use it when the interaction itself is the point of the code, as with sending a message, because there is no returned value that would show it.

2Counting Calls: times, never, atLeastOnce

Let us take a PaymentService that charges a card and sends a receipt, and retries the charge once if the gateway times out.

Java
// gateway.charge() times out on the first attempt, succeeds on the second
service.pay(order);
verify(gateway, times(2)).charge(order.card(), order.total()); // passes: one retry
verify(receipts).send(order.customer()); // passes: times(1) by default
verify(refunds, never()).refund(any()); // passes: no refund on success
verify(audit, atLeastOnce()).record(any()); // passes: 1 or more entries
verify(gateway, atMost(3)).charge(any(), any()); // passes: 2 is at most 3

Notice that each line expresses one business fact. times(2) says the retry happened exactly once, which is important because charging three times would be a serious bug. never() says no refund was issued, which is a cleaner way to write times(0). atLeastOnce() is for cases where the exact number does not matter, such as logging or audit entries that may grow. atLeast(n) and atMost(n) cover ranges.

3Checking Order With inOrder

Sometimes the order matters. We must charge the card before sending the receipt, never the other way round. A plain verify() cannot express this, because it only checks that calls exist. inOrder() creates an InOrder object for one or more mocks, and verifications through it must match calls in the order we write them.

Java
InOrder order = inOrder(gateway, receipts);
order.verify(gateway, times(2)).charge(any(), any());
order.verify(receipts).send(order.customer()); // passes: the receipt came after the charges
// If the code sent the receipt first:
// VerificationInOrderFailure: Wanted but not invoked ... wanted anywhere AFTER following interaction
Step throughThe order inOrder(gateway, receipts) checks
PaymentServicegatewayreceipts
charge(card, total): timeout
charge(card, total): ok
send(customer)

3 messages in order. Play them, or step through them one at a time.

order.verify() passes only if the receipt is sent after both charges.

Notice that the InOrder spans two different mocks. That is its main value: it checks the order of the whole conversation, not only calls on one object. It is also flexible: calls we do not mention can appear in between, so the test checks only the order we care about.

4What verifyNoMoreInteractions Really Checks

verifyNoMoreInteractions(mock) passes only if every call the mock received has been verified by an earlier verify() in the test. It sounds like a thorough safety net, but look at what happens over time.

Java
verify(gateway, times(2)).charge(any(), any());
verifyNoMoreInteractions(gateway); // passes today
// Three months later someone adds a harmless health check before charging:
// gateway.isAvailable();
// verifyNoMoreInteractions(gateway) now fails with NoInteractionsWanted,
// although the payment logic is unchanged and correct.

The test now fails because of an implementation detail, not a bug. Repeated across a suite, this turns every small refactor into a long list of red tests that teams fix by adding more verify() lines, and the tests end up mirroring the implementation line by line. That kind of test catches few real bugs and makes change expensive.

5When verifyNoMoreInteractions Is Worth It

There are cases where the absence of other calls is the requirement itself. A dry-run mode of a billing job must not call the payment gateway at all. A read-only report must never write to the repository. A security filter must not reach the downstream service for a rejected request. In those cases, a strict check is exactly right, and verifyNoInteractions(gateway) states it directly: the mock must have received no calls at all.

So the rule is to verify the interactions that are the purpose of the code, with the counts and order that matter to the business, and to use the 'nothing else' checks only when 'nothing else' is a real rule that someone could break.

Java
service.runBilling(Mode.DRY_RUN);
verifyNoInteractions(gateway); // passes: dry run never touches the payment gateway
Verification modes at a glance
ModeChecksTypical use
times(n), default times(1)exactly n callscharge once, retry exactly once
never()no matching callno refund on success
atLeastOnce(), atLeast(n), atMost(n)a range of callsaudit or log entries
inOrder(a, b)relative order across mockscharge before receipt
verifyNoMoreInteractions(m)every call was verifiedrarely; strict protocols
verifyNoInteractions(m)no calls at alldry runs, rejected requests

6Follow-up questions

Is never() the same as verifyNoInteractions()?

No. never() is about one method with matching arguments; the mock may receive other calls. verifyNoInteractions() fails if the mock received any call at all.

Does inOrder require all calls to be verified?

No. It checks that the calls we verify appear in that relative order, and other calls may come in between. There is also calls(n) for non-greedy in-order checks, which is rarely needed.

Should I verify a call whose result the test already asserts?

Usually not. If the service returns a user loaded from the repository and we assert on the user, verifying repo.findById() adds nothing except coupling to how the user was loaded.

7Conclusion

In this answer, we saw that verify() checks recorded calls, times(1) by default, and that times(n), never(), atLeastOnce(), atLeast() and atMost() express how many calls happened, while inOrder() checks their order across mocks. verifyNoMoreInteractions() fails on any unverified call, which makes tests break when harmless calls are added, so we keep it, and verifyNoInteractions(), for cases where the absence of other calls is a real requirement.

What to say in the interviewAbout 70 seconds aloud

Open with

verify checks the calls a mock recorded after the code ran. With no mode it means exactly once. times(2) means exactly two calls, which I would use to check that a payment retried once and not more. never() means the call did not happen with those arguments, and atLeastOnce, atLeast and atMost check ranges, which suit things like audit entries where the exact count is not the point. inOrder creates an InOrder for one or more mocks, and verifying through it checks the relative order of calls, even across different mocks, for example that the card is charged before the receipt goes out.

Then, if they want more

Other calls may happen in between. verifyNoMoreInteractions fails if the mock received any call that I did not verify. I avoid it in general, because it couples the test to every call the implementation makes, so a harmless new call, like a health check, breaks the test even though behaviour is correct.

I use it, or better verifyNoInteractions, only when the absence of calls is a real requirement, such as a dry-run job that must never call the payment gateway.

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.
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 "What do verify(mock, times(2)), never(), atLeastOnce() and inOrder check in Mockito?...": https://scrivio-interview-library.netlify.app/java/mocking/0407a40729/