skip to content

What is the difference between getValue() and getAllValues() on an ArgumentCaptor, and how do you capture arguments across multiple calls?

level: middleimportance: should knowfreq 50%

answer

  1. getValue() = last captured
  2. getAllValues() = full list, call order
  3. times(n) needed to verify multiple calls
  4. Captor is an accumulator
  5. Single call → getValue, many → getAllValues

basics

~10 s

getValue() returns the last captured argument; getAllValues() returns every captured argument as a list in call order. Use getAllValues() with verify(mock, times(n)) to check a sequence of calls.

solid answer

~40 s

A single captor accumulates every value it captures. getValue() returns just one — specifically the last captured value — so it's meant for the single-call case. getAllValues() returns a List of all captured values in the order the calls happened. When the mock's method is invoked several times, verify it with a multiplicity like verify(mock, times(3)).method(captor.capture()), which captures all three arguments; then getAllValues() gives you the list to assert on. Calling getValue() when there were multiple captures still works but only yields the last one, which is a common source of confusing test results. So: one call → getValue(); many calls → times(n) + getAllValues().

code

java · 9 lines
java
service.publish("a");
service.publish("b");
service.publish("c");

verify(bus, times(3)).send(captor.capture());

List<String> all = captor.getAllValues();
assertThat(all).containsExactly("a", "b", "c");
assertThat(captor.getValue()).isEqualTo("c"); // last one

go deeper

for a junior

Knows getValue() gives one value and getAllValues() gives a list.

for a middle

Correctly pairs verify(mock, times(n)) with getAllValues() and knows getValue() yields the last of many.

for a senior

Explains the captor-as-accumulator model, call-order semantics, and the verification-multiplicity failure mode when times(n) is wrong.

for a principal

Advises on test clarity (captor-per-concern), warns against fragile sequence assertions, and reviews for the getValue-returns-last gotcha.

## A captor is an accumulator Each time a captor's `capture()` is matched against a mock invocation, the actual argument is **appended** to an internal list inside the captor. So after a verification, the captor may hold one value or many. ## getValue() — the single (or last) value ```java String getValue() // returns the most recently captured value ``` `getValue()` returns the **last** captured argument. In the common single-call test this is exactly what you want. If the method was actually called multiple times and you call `getValue()`, you silently get only the **last** one — a frequent gotcha when a test author expected the first. ## getAllValues() — every value, in order ```java List<String> getAllValues() // all captured values, call order ``` Use this when the collaborator is called more than once and you want to assert on the **whole sequence**. ## Capturing across multiple calls To capture all arguments from several invocations, verify with a multiplicity that matches the number of calls: ```java service.publish("a"); service.publish("b"); service.publish("c"); verify(bus, times(3)).send(captor.capture()); List<String> all = captor.getAllValues(); assertThat(all).containsExactly("a", "b", "c"); assertThat(captor.getValue()).isEqualTo("c"); // last one ``` If you write `verify(bus).send(captor.capture())` (implicitly `times(1)`) when there were three calls, the verification itself **fails** with a too-many-invocations error — so you must use `times(3)` (or `atLeast`, `atMost`, etc.). ## Capturing across different mocks/methods You can reuse one captor across multiple verify calls (even on different methods/mocks); all captured values pile into the same list in the order the verifications execute. That's occasionally handy but can be confusing — prefer a captor per logical thing you're capturing. ## Summary table | Need | Use | |---|---| | Inspect the only argument | `getValue()` | | Inspect the last of many | `getValue()` | | Inspect all arguments in order | `getAllValues()` + `verify(mock, times(n))` |

  • If a mock method was called 3 times and you call getValue(), what do you get?
    The last (third) captured value. To inspect all three, use getAllValues() after verify(mock, times(3)).
  • What ordering does getAllValues() use?
    The order in which the captures occurred (call/verification order), not any sorted order.

saying these in an interview costs you the question

  • Expecting getValue() to return the first of several captures (it returns the last)
  • Using plain verify(mock) (times 1) when the method was called multiple times — verification fails
  • Assuming getAllValues() is sorted — it's in call order, not sorted
  • Thinking you need a new captor per call — one captor accumulates all

context