Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

When is argThat with a custom matcher better than a captor, and what are the trade-offs?

argThat is better when we want to state the expected argument inline, inside when() or verify(), especially in stubs where the answer depends on the argument and a captor does not fit. A captor is better when we want to assert on a complex object, because AssertJ assertions on the captured value give a far clearer failure message than a lambda that just returned false. And when the value object has a proper equals(), plain equality beats both.

Why this question is asked

Every Mockito user eventually needs to check an argument that is not a simple value: an Order with ten fields, an event with a generated id. Mockito offers two tools for it, a custom matcher through argThat and an ArgumentCaptor, and teams often argue about which one to use. Interviewers ask this to see whether we think about tests as something people will read when they fail, not only when they pass. In this answer, we will recap both tools, compare them on the same example, look closely at failure messages, and finish with a simple rule for choosing.

1A Quick Recap of the Two Tools

argThat takes an ArgumentMatcher, a small functional interface whose matches(T) method returns true when an argument is acceptable. We usually write it as a lambda. It is a matcher like eq() or any(), so it can be used both in when(), to decide which calls a stub answers, and in verify(), to decide which recorded calls count.

An ArgumentCaptor works differently. It matches any argument, but it remembers the value during verify(), and then we inspect that value with ordinary assertions in the next lines of the test. So argThat describes the expected argument up front, while a captor grabs the actual argument and lets us check it afterwards.

2The Same Check Written Both Ways

Suppose an OrderService publishes an OrderPlaced event, and we want to check that the event carries the customer id 42 and a total of 99.50.

Java
// With argThat: the expectation sits inside verify()
verify(publisher).publish(argThat(e ->
e.customerId() == 42 && e.total().compareTo(new BigDecimal("99.50")) == 0));
// With a captor: grab the event, then assert on it
ArgumentCaptor<OrderPlaced> captor = ArgumentCaptor.forClass(OrderPlaced.class);
verify(publisher).publish(captor.capture());
assertThat(captor.getValue())
.returns(42L, OrderPlaced::customerId)
.returns(new BigDecimal("99.50"), OrderPlaced::total);

When the code is correct, both versions pass and both are fine to read. argThat is a little shorter and keeps everything in one statement. The real difference shows up only when the test fails, and that is the moment a test exists for.

3What Each One Prints When It Fails

Now imagine a bug makes the total 95.00. With argThat, Mockito knows only that our lambda returned false. It reports that the wanted call was not found, prints our matcher as something like a lambda reference, and lists the actual call with the event's toString(). We have to read the whole event and work out ourselves which field is wrong.

With the captor and AssertJ, the verify() line passes, because publish() was called once. Then the assertion fails and names the field: it expected 99.50 from total but found 95.00. That is the difference between a failure that says 'something is wrong with this object' and one that says exactly what is wrong.

Java
// argThat failure (shape of the message, details vary by version):
// Argument(s) are different! Wanted:
// publisher.publish(<custom argument matcher>);
// Actual invocations have different arguments:
// publisher.publish(OrderPlaced[customerId=42, total=95.00, ...]);
// captor + AssertJ failure:
// Expecting value to be equal to: 99.50 but was: 95.00
// (when checking OrderPlaced::total)
argThat compared with a captor
QuestionargThatCaptor with AssertJ
Works in when() stubs?yes, picks which calls the stub answersnot meaningfully, matches anything
Works in verify()?yesyes
Failure messagewanted call not found, whole object printednames the field and both values
Several conditionsone boolean, all or nothingone assertion per field
Lines of codefewera few more

We can improve argThat's message by implementing ArgumentMatcher as a named class with a toString() that describes the expectation, but that is more code than the captor version, and it still cannot say which part failed.

There is a second subtlety. If the lambda throws, for example a NullPointerException because the event's total is null, the exception escapes from inside Mockito's matching and the stack trace points into the matcher, which is confusing to read. Assertions on a captured value fail with a normal assertion error.

4Where argThat Is the Better Choice

argThat really earns its place in stubbing. When the mock should answer differently depending on the argument, a captor cannot help, because a captor matches everything. argThat lets us say 'return this only for orders over 100'.

Java
when(fraudCheck.score(argThat(o -> o.total().compareTo(new BigDecimal("100")) > 0)))
.thenReturn(Score.HIGH);
when(fraudCheck.score(argThat(o -> o.total().compareTo(new BigDecimal("100")) <= 0)))
.thenReturn(Score.LOW);
fraudCheck.score(orderOf("150.00")); // HIGH
fraudCheck.score(orderOf("20.00")); // LOW

It is also handy in verify() when the condition is a single, simple fact, such as argThat(list -> list.size() == 3), where a failure message cannot say much more anyway. And it is the natural choice when we want to verify that a call with a certain argument never happened: verify(publisher, never()).publish(argThat(e -> e.total().signum() < 0)).

5Prefer Plain Equality When the Object Supports It

Before reaching for either tool, we should ask whether equals() already does the job. If OrderPlaced is a record or a value class with a correct equals(), and we can predict every field, then verify(publisher).publish(new OrderPlaced(42L, new BigDecimal("99.50"))) is the shortest and clearest check, and Mockito's failure message shows the wanted and actual objects side by side.

So the order of preference is simple. Use equality when the whole value is predictable. Use a captor with AssertJ when only some fields matter or some fields are unpredictable, such as a timestamp or a random id. Use argThat when the stub's answer must depend on the argument, or when the condition is one simple predicate. Notice that BigDecimal deserves care here: new BigDecimal("99.5") is not equal to new BigDecimal("99.50") with equals(), which is why the examples above use compareTo() or the exact scale.

6Follow-up questions

Can I use AssertJ inside argThat to get a better message?

We can call assertions inside the lambda, and the assertion error will escape with a good message, but this abuses the matcher: Mockito may call it for other recorded calls too, and the first non-matching call throws instead of letting Mockito try the next one. A captor is the cleaner way to get assertion-quality messages.

Is there a matcher for a field, without a lambda?

Mockito has refEq(), which compares objects field by field through reflection, optionally excluding some fields. It works without an equals() method, but it is fragile when fields are renamed and its message is no clearer, so most teams prefer a captor.

What about Hamcrest matchers?

MockitoHamcrest.argThat() accepts a Hamcrest Matcher, and Hamcrest matchers describe themselves, so the failure message improves. It is a good option in code bases that already use Hamcrest.

7Conclusion

In this answer, we saw that argThat states the expected argument inline and is the right tool in stubs whose answer depends on the argument, or for one simple condition in verify(). A captor grabs the real argument, and with AssertJ it gives a failure that names the wrong field, which makes it better for complex objects. When the value object has a sound equals() and every field is predictable, plain equality is simpler than both.

What to say in the interviewAbout 70 seconds aloud

Open with

argThat and a captor solve the same problem from opposite ends. argThat puts the expectation inline, as a predicate, inside when or verify. A captor grabs the actual argument during verify, and then I assert on it in normal code.

Then, if they want more

My main criterion is the failure message. When an argThat lambda returns false, Mockito can only say the wanted call was not found and print the whole actual object, so I have to hunt for the wrong field. With a captor and AssertJ, the assertion says, for example, expected total 99.50 but was 95.00.

So for complex objects in verification I prefer a captor. argThat is better in stubbing, when the mock should answer differently depending on the argument, because a captor matches everything and cannot drive a stub. It is also fine for a single simple condition, or inside never().

And before either, I check whether the object has a proper equals, like a record. If every field is predictable, verifying with the expected object directly is the clearest of all.

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 "When is argThat with a custom matcher better than a captor, and what are the trade-offs?": https://scrivio-interview-library.netlify.app/java/mocking/7a979b8a03/