How to verify the order of calls using Mockito

November 22, 2020 No comments Mockito Verify Order Methods

1. Introduction

In this article, we are going to present a way to verify the order of method calls in Mockito tests. We will show a special use of an InOrder object in conjunction with the verify method

2. Verify the order of method calls

To verify the order of calls Mockito provides a special object InOrder.

Let's just straight to the example, to better understand how to use it:

package com.frontbackend.libraries.mockito;

import static org.mockito.Mockito.inOrder;

import java.util.Map;

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

@RunWith(MockitoJUnitRunner.class)
public class MockitoInOrderTest {

    @Mock
    private Map<String, String> map;

    @Test
    public void testInOrderOnASingleMockedObject() {
        map.put("one", "val1");
        map.put("two", "val2");

        InOrder inOrder = inOrder(map);

        inOrder.verify(map)
               .put("one", "val1");

        inOrder.verify(map)
               .put("two", "val2");
    }
}

In this example, we used the InOrder class of Mockito to verify the order of method calls on the mocked Map object.

If the order is not correct we will get an exception similar to this:

org.mockito.exceptions.verification.VerificationInOrderFailure: 
Verification in order failure
Wanted but not invoked:
map.put("one", "val1");
-> at com.frontbackend.libraries.mockito.MockitoInOrderTest.testInOrderOnASingleMockedObject(MockitoInOrderTest.java:27)
Wanted anywhere AFTER following interaction:
map.put("two", "val2");
-> at com.frontbackend.libraries.mockito.MockitoInOrderTest.testInOrderOnASingleMockedObject(MockitoInOrderTest.java:22)

This example was simple, and use only a single mock object. However, Mockito.inOrder(...) allows configuring more than one mock.

Let's now check a more complex example with two mock objects:

package com.frontbackend.libraries.mockito;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Map;

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

@RunWith(MockitoJUnitRunner.class)
public class MockitoInOrderTest {

    @Mock
    private Map<String, String> map;

    @Mock
    private List<String> list;

    @InjectMocks
    private SomeClass someClass;

    static class SomeClass {
        private final Map<String, String> map;
        private final List<String> list;

        SomeClass(Map<String, String> map, List<String> list) {
            this.map = map;
            this.list = list;
        }

        public void doSomething() {
            map.put("first", "value1");
            list.add("second");
            map.put("test", list.get(0));
        }
    }

    @Test
    public void testInOrderOnSeveralMockObjects() {
        // Given
        when(list.get(0)).thenReturn("second");

        // When
        someClass.doSomething();

        // Then
        InOrder inOrder = inOrder(map, list);

        inOrder.verify(map)
               .put(anyString(), anyString());
        inOrder.verify(list)
               .add("second");
        inOrder.verify(list)
               .get(0);
        inOrder.verify(map)
               .put("test", "second");
    }
}

In this example, we have SomeClass with the doSomething() method that calls methods on mocked objects. In the Mockio.inOrder(..) method we are passing two mock objects: map and list. Then we used the inOrder.verify(...) method for verification the order of method calls and input params.

3. Conclusion

In this article, we presented a simple method to verify the order of method calls. This kind of verifications is useful in tests where the calling order of methods is also important not only the number.

Test snippets used in this article are available in our GitHub repo.

{{ message }}

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