1. Introduction
In this article, we will present how to capture all arguments used in multiple method calls using the Mockito testing framework.
2. Using @Captor
annotation
Mockito provides a special @Captor functionality used to capture the arguments passed to a method in mock object. This annotation always goes hand in hand with ArgumentCaptor
.
Let's check a simple unit test that uses @Captor
and ArgumentCaptor
:
In this example, we create a mock object with a list of Person
objects. Then we add two persons to the list and using getAllValues()
we get arguments from all method invocations.
This test answering the question given in the article title: to capture all arguments we used the argumentCaptor.getAllValues()
method.
3. Conclusion
In this short article, we presented how to capture all arguments of a method that was called multiple times. Luckily Mockito holds all information about method invocations for future assertions.
As usual the code used in this article is available under GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}