4.1 Method parameters

The previous rules added to the mock object dealt with methods without parameters. And how to create rules for methods with parameters? Moreover, I would like to create rules so that for some parameter values ​​there is one result, and for others - another.

That's how you can do it too. If you want the method to return something specific with a certain parameter, then the rule can be written like this:

Mockito.doReturn(result).when(an object).method name(parameter);

Let's take a look at an example to understand better. Let our List return the name Ivan when requesting the 10th element, and the name Marya when requesting the 500th. No sooner said than done.

@ExtendWith(MockitoExtension.class)
class ParamsTest {
    @Mock
    List mockList;

    @Test
    public void whenMockAnnotation() {
        //adding the first rule
        Mockito.doReturn("Ivan").when(mockList).get(10);
        //adding a second rule
        Mockito.doReturn("Maria").when(mockList).get(500);

        assertEquals("Ivan", mockList.get(10));
        assertEquals("Maria", mockList.get(500));

    }
}

4.2 Parameter templates

And immediately cunning colleagues will ask me the question: “What if the method requires arguments, but for any values ​​it must return the same result?”. Let's not write:

Mockito.doReturn("Ivan").when(mockList).get(1);
Mockito.doReturn("Ivan").when(mockList).get(2);
Mockito.doReturn("Ivan").when(mockList).get(99);

No, no one is forcing you to write like that. If you want to add a rule to a mock object that works for a method with any arguments, then there is a special object for this:

Mockito.any()

Our example with it would be written like this:

Mockito.doReturn("Ivan").when(mockList).get(any(int.class));

There are a couple of nuances here. The object Mockito.any()has type Object, so there are analogues for parameters of different types:

Method Parameter type
1 any() Object including null
2 any(ClassName.class) class name
3 anyInt() int
4 anyBoolean() boolean
5 anyDouble() double
6 anyList() List

More correctly, our example would look like this:

Mockito.doReturn("Ivan").when(mockList).get(anyInt());

4.3 doAnswer() method

We got to the complex behavior of virtual methods. Sooner or later there will be a situation when you want this virtual method to have complex behavior. For example, it should return values ​​depending on the parameters, convert the string to upper case.

There is a special method for this - doAnswer(), which is passed a function that does what you need:

Mockito.doAnswer(function).when(an object).method name(parameter);

Let's make the get()class method Listreturn the square of the argument passed to it. And write the following program:

@ExtendWith(MockitoExtension.class)
class DoAnswerTest {
    @Mock
    List mockList;

    @Test
    public void whenMockAnnotation() {
        Mockito.doAnswer(invocation -> {
            int parameter = invocation.getArgument(0);
            return parameter * parameter;
        }).when(mockList).get(anyInt());

        assertEquals(100, mockList.get(10));
        assertEquals(25, mockList.get(5));
    }
}

We defined the function using an object of the Answer class.