How to stub a method to return different objects on subsequent invocations using Mockito

December 25, 2020 No comments Mockito Mock Stub Java

1. Introduction

In this article, we will present a way to stub a method using Mockito to return different values on subsequent calls. This configuration is useful for testing various scenarios with nondeterminate method responses.

2. Using when(...).thenReturn(...) method

The thenReturn(...) method could take more than one argument. The first value will be returned on the first method call, then on the next call the second value will be returned, and so on. The last value will be returned repeatedly once all the other values are used up.

Let's check the example test:

package com.frontbackend.libraries.mockito;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MockitoDifferentResponses {

    interface SomeMock {
        String someMethod();
    }

    @Mock
    private SomeMock someMock;

    @Test
    public void test() {
        when(someMock.someMethod()).thenReturn("First Value", "Second Value");

        String firstResponse = someMock.someMethod();
        String secondResponse = someMock.someMethod();

        assertEquals("First Value", firstResponse);
        assertEquals("Second Value", secondResponse);
    }
}

3. Using doReturn method

In case we want to stub a void method or use spy instead of mock we should use doReturn(...).when(...) syntax.

The following example presents how to use different return values on spied object:

package com.frontbackend.libraries.mockito;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MockitoDifferentResponses {

    static class SomeSpy {
        String spyMethod() {
            return "spy";
        }
    }

    @Spy
    private SomeSpy someSpy = new SomeSpy();

    @Test
    public void testSpyMethodWithDifferentResponses() {
        doReturn("First Value").doReturn("Second Value")
                               .when(someSpy)
                               .spyMethod();

        String firstResponse = someSpy.spyMethod();
        String secondResponse = someSpy.spyMethod();

        assertEquals("First Value", firstResponse);
        assertEquals("Second Value", secondResponse);
    }
}

4. Conclusion

In this article, we presented how to mock a method that will return different values according to invocation count. This could be useful to test different behavior of the tested object and check how it will react to non-deterministic input values.

As usual code snippets used in this article are available on GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}