How to use Mockito ArgumentCaptor

In this post, I am going to explain how we can use ArgumentCaptor to test out an internal object which is getting passed to some other class.

Consider that you have a Main class which has a method called “populateAndSavePersonData” which is used to construct the Person details(Person Object) and calls the “savePersonData” method from Dao class to populate the data into DB.  Now you want to test out the Person object and its values since it’s not getting returned from this class. So we have to either update the class to return the person object or we can use ArgumentCaptor to capture that object.

Refer the below example to know how we can do that.

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;


public class Person implements Serializable {

    private String firstName;

    private String lastName;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String toString() {
        return ToStringBuilder.reflectionToString(this);

    public boolean equals(Object o) {
        return EqualsBuilder.reflectionEquals(this, o);

    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);

} and

public class Main {

    private Dao dao;
    public Main(Dao dao){
        this.dao = dao;
    public void populateAndSavePersonData(String firstName, String lastName) {

        Person person = new Person();

    public class Dao {

        public void savePersonData(Person person) {
            // Code to save the data into DB
In our test method, We have to create an ArgumentCaptor object and pass the appropriate object which you want to capture(Person). Then you have to call the actual method with the appropriate arguments. After that call “verify” method on Mockito object and pass the dao object and call the appropriate method(“savePersonData”) which gets called in the “populateAndSavePersonData” method. Finally, we have to get the value from ArgumentCaptor and check the value in it.

import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

public class MainTest {

    public void populateAndSavePersonData(){
        Main.Dao dao = Mockito.mock(Main.Dao.class);
        Main main = new Main(dao);
        ArgumentCaptor personArgumentCaptor = ArgumentCaptor.forClass(Person.class);
        Person person = personArgumentCaptor.getValue();


2 thoughts on “How to use Mockito ArgumentCaptor

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s