Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the differences between getValue() and getAllValues() in ArgumentCaptor


What are the differences between getValue() and getAllValues() in ArgumentCaptor


In Mockito's `ArgumentCaptor`, both `getValue()` and `getAllValues()` are used to retrieve captured arguments, but they serve different purposes based on the scenario.

getValue()

The `getValue()` method is used to retrieve the latest captured argument when a method is invoked. If the method under test is called multiple times, `getValue()` will only return the argument from the last invocation. This method is particularly useful when you are certain that the method is called only once, or when you are interested in the last argument passed to it.

For example, if you have a scenario where a method is called once, you can use `getValue()` to assert the argument passed:

java
ArgumentCaptor captor = ArgumentCaptor.forClass(String.class);
verify(mockedList).add(captor.capture());
String capturedValue = captor.getValue();
assertEquals("Expected Value", capturedValue);

getAllValues()

The `getAllValues()` method, on the other hand, returns a list of all captured arguments in the order they were captured. This is useful when the method under test is invoked multiple times, and you need to verify all the arguments passed during these invocations.

Here's an example where `getAllValues()` is used to capture multiple arguments:

java
ArgumentCaptor captor = ArgumentCaptor.forClass(String.class);
mockedList.add("one");
mockedList.add("two");
verify(mockedList, times(2)).add(captor.capture());
List allValues = captor.getAllValues();
assertEquals("one", allValues.get(0));
assertEquals("two", allValues.get(1));

In summary, `getValue()` is used for single invocations or when you're interested in the last argument, while `getAllValues()` is used when you need to verify all arguments from multiple invocations.

Best Practices

- Use `getValue()` when you expect a single invocation or are only interested in the last argument passed.
- Use `getAllValues()` when the method is called multiple times and you need to verify all arguments.
- Always specify the type when creating an `ArgumentCaptor` to avoid type ambiguity.
- Limit the scope of capturing to only the relevant arguments for your test scenario.

Citations:
[1] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentCaptor.getAllValues
[2] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentCaptor.getValue
[3] https://www.javacodegeeks.com/2024/11/mastering-mockito-argument-captors.html
[4] https://springframework.guru/argumentcaptor-in-mockito/
[5] https://stackoverflow.com/questions/36253040/example-of-mockitos-argumentcaptor
[6] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentCaptor.ArgumentCaptor
[7] https://groups.google.com/g/mockito/c/JsMU-_cOIs8
[8] https://www.baeldung.com/mockito-argumentcaptor
[9] https://mincong.io/2019/12/15/mockito-argument-captor/
[10] https://www.digitalocean.com/community/tutorials/mockito-argumentcaptor-captor-annotation