1. Overview

In this tutorial, we’ll look at how we can return an argument which is passed to a mocked method. For this purpose, we will use Mockito’s built-in functionality.

2. Sample Application

Firstly, let’s look at our sample application.

We have a PersonRepository class. Note that PersonService includes PersonRepository as a dependency:

public class PersonService {

    private final PersonRepository personRepository;

    public PersonService(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }
...
    public Person select(Person first, Person second, Person third) {
        return personRepository.select(first, second, third);
    }
}

public class PersonRepository {
...
    public Person select(Person first, Person second, Person third) {
        return first;
    }
}

 

3. Returning the First Argument

Mockito provides built-in support for getting method arguments. Hence we will be using AdditionalAnswers class which contains different implementations of Answer interface.

Firstly, AdditionalAnswers.returnsFirstArg() helps us returning the first argument:

@InjectMocks
private PersonService personService;

@Mock
private PersonRepository personRepository;

@Test
public void shouldReturnFirstArg() {
    Person firstPerson = new Person("first");
    Person secondPerson = new Person("second");
    Person thirdPerson = new Person("third");
    Mockito.when(personRepository.select(firstPerson, secondPerson, thirdPerson)).then(AdditionalAnswers.returnsFirstArg());

    Person actual = personService.select(firstPerson, secondPerson, thirdPerson);

    Assertions.assertThat(actual).isEqualTo(firstPerson);
}

Here, since we’re using AdditionalAnswers.returnsFirstArg(), the return value will be firstPerson.

4. Returning the Second Argument

Similarly, AdditionalAnswers.returnsSecondArg() helps us returning the second argument:

@InjectMocks
private PersonService personService;

@Mock
private PersonRepository personRepository;

@Test
public void shouldReturnSecondArg() {
    Person firstPerson = new Person("first");
    Person secondPerson = new Person("second");
    Person thirdPerson = new Person("third");
    Mockito.when(personRepository.select(firstPerson, secondPerson, thirdPerson)).then(AdditionalAnswers.returnsSecondArg());

    Person actual = personService.select(firstPerson, secondPerson, thirdPerson);

    Assertions.assertThat(actual).isEqualTo(secondPerson);
}

Since we’re using AdditionalAnswers.returnsSecondArg(), the return value will be secondPerson.

5. Returning the Last Argument

Next, AdditionalAnswers.returnsLastArg() helps us returning the last argument:

@InjectMocks
private PersonService personService;

@Mock
private PersonRepository personRepository;

@Test
public void shouldReturnLastArg() {
    Person firstPerson = new Person("first");
    Person secondPerson = new Person("second");
    Person thirdPerson = new Person("third");
    Mockito.when(personRepository.select(firstPerson, secondPerson, thirdPerson)).then(AdditionalAnswers.returnsLastArg());

    Person actual = personService.select(firstPerson, secondPerson, thirdPerson);

    Assertions.assertThat(actual).isEqualTo(thirdPerson);
}

Here, we’re using AdditionalAnswers.returnsLastArg(). As a result, the return value will be thirdPerson.

6. Returning the Argument at Position

Finally, AdditionalAnswers.returnsArgAt(index) helps us returning the argument at given index:

@InjectMocks
private PersonService personService;

@Mock
private PersonRepository personRepository;

@Test
public void shouldReturnArgAt() {
    Person firstPerson = new Person("first");
    Person secondPerson = new Person("second");
    Person thirdPerson = new Person("third");
    Mockito.when(personRepository.select(firstPerson, secondPerson, thirdPerson)).then(AdditionalAnswers.returnsArgAt(1));

    Person actual = personService.select(firstPerson, secondPerson, thirdPerson);

    Assertions.assertThat(actual).isEqualTo(secondPerson);
}

Here, since we’re calling AdditionalAnswers.returnsArgAt() with the value of 1, the return value will be secondPerson.

7. Summary

In this tutorial, we’ve investigated returning given arguments on mocked methods.

Check out the source code for all example over on Github.

Leave a Reply

Close Menu