Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Verification

Verifying Call Order with InOrder

Use Mockito InOrder to verify that calls happen in a given sequence, across one or several mocks, and learn what it ignores, when order matters and how it fails.

Plain verify calls do not care about order. verify(a).first() followed by verify(b).second() passes whether first ran before or after second. For many programs that is fine, but some sequences are part of the contract: open the connection, then send, then close; lock, update, unlock; debit the account, then credit the other one, then commit. To test a sequence, Mockito has InOrder.

Real Mockito code is read-only. Runnable examples use the Mini stand-in from What Is Mockito?, which provides inOrder(...) and inOrder.verify(mock). The stand-in supports the basic form only (no times inside inOrder).

The pattern

Java
import static org.mockito.Mockito.*;
import org.mockito.InOrder;

InOrder inOrder = inOrder(accounts, ledger);       // the mocks whose order interests you

inOrder.verify(accounts).debit("A", 100);
inOrder.verify(accounts).credit("B", 100);
inOrder.verify(ledger).commit();

Three steps: create an InOrder object and list the mocks that take part, then write your verify calls on the InOrder (not on Mockito), in the order you expect them to have happened. Each verification must find its call after the previously verified one. If the code under test executed credit first, the second line fails.

What the example shows:

  • It works across several mocks: accounts and ledger share one timeline. Mockito records every interaction in the order it happened.
  • It is not strict about gaps: other calls between the verified ones are allowed. InOrder only checks that the verified calls appear in the given relative order. To forbid gaps, add inOrder.verifyNoMoreInteractions() at the end.
  • You can verify the same method twice to say "A, then something else, then A again".
  • inOrder.verify(mock, times(2)).call() and the other modes work as well in real Mockito.

Try it: a money transfer

The TransferService below must debit, then credit, then commit. The runnable program checks the order across two mocks.

Java InOrder across two 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.

When the order is wrong

Now the same test against a service with a bug: it commits before crediting. Every individual call happens exactly once, so plain verification is happy, and only InOrder notices.

Java InOrder catches a swapped sequence

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.

In real Mockito the failure is a VerificationInOrderFailure whose message says "Verification in order failure" and lists the wanted call and the calls that actually happened, usually with the calls already verified marked.

When to use InOrder

Order verification is the most implementation-coupled kind of verification, so use it only where the sequence is the business rule:

  • resource lifecycles (open before read, close last);
  • transactions (begin, work, commit or rollback);
  • locks (lock before update, unlock after, even on failure);
  • protocols (handshake before data);
  • event pipelines where consumers rely on started coming before finished.

Do not use it to document the order of unrelated steps, such as "load user, then load settings". If those lookups swap places in a refactoring, nothing breaks, but your test would.

A related tip: when only one mock is involved and you need the order of its calls, you can create inOrder(mock) with just that mock. That is the usual form for a lifecycle such as connection.open(); connection.send(..); connection.close();.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do plain verify calls not detect a wrong order?

    Plain verification looks for matching calls anywhere in the history, so the sequence is invisible to it.
  2. 2. Where do you write the verify calls when checking order?

    You create an InOrder with the mocks involved and call verify on that object, in the expected sequence.
  3. 3. Does InOrder forbid other calls between two verified calls?

    InOrder checks relative order only. Gaps are allowed until you ask for no more interactions.
  4. 4. Which scenario is a good fit for InOrder?

    The sequence is part of the contract for transactions, locks and similar lifecycles, so verifying it makes sense.

Summary

  • InOrder inOrder = inOrder(mocks...) plus inOrder.verify(mock).call() checks relative order across one or several mocks.
  • Gaps between verified calls are allowed; use inOrder.verifyNoMoreInteractions() to close them.
  • Plain verification cannot detect a swapped sequence, so use InOrder when the order is a business rule.
  • Keep order checks for lifecycles, transactions, locks and protocols; they couple tests to the implementation.

Next: Argument Matchers