Mockito – Verify multiple method calls with different arguments

Learn to write unit test which invokes a method multiple times with different arguments – and then verifies the method invocations and method arguments separately.

1. Verify multiple method calls with different arguments

The given unit test has mocked the HashMap class and invokes in put(key, value) code twice. It then verifies that method had been invoked twice. The test further verifies all the different method arguments separately.

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

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MockitoExample 
{
	@Mock
	HashMap<String, Integer> hashMap;

	@Captor
	ArgumentCaptor<String> keyCaptor;

	@Captor
	ArgumentCaptor<Integer> valueCaptor;

	@Test
	public void saveTest() 
	{
		hashMap.put("A", 10);
		hashMap.put("B", 20);
		
		//1. Verify method was invoked N times
		
		Mockito.verify(hashMap, times(2)).put(keyCaptor.capture(), valueCaptor.capture());

		List<String> keys = keyCaptor.getAllValues();
		List<Integer> values = valueCaptor.getAllValues();
		
		//2. Verify method argument values as list

		assertEquals(Arrays.asList("A", "B"), keys);
		assertEquals(Arrays.asList(Integer.valueOf(10), Integer.valueOf(20)), values);
		
		//3. Verify method arguments separately
		
		assertEquals("A", keys.get(0));
		assertEquals("B", keys.get(1));
		
		assertEquals(Integer.valueOf(10), values.get(0));
		assertEquals(Integer.valueOf(20), values.get(1));
	}
}

2. Explanation

To capture and verify all the method arguments passed to a method when it is invoked multiple times, we shall follow below steps:

  • Use Mockito.verify(mock, times(n)) to verify if method was executed 'n' times.
  • Create as many ArgumentCaptor instances as the number of arguments in the method. In above example, we tested the HashMap which work on key-value pairs, so we created two ArgumentCaptor instances – one for key and second for value.
  • Use ArgumentCaptor.getAllValues() to retrieve all the values passed to one method parameter during all executions. It returns a List of passed argument values.
  • We can use assertEquals(expected, result) to verify that expected multiple arguments match with the retrieved values from ArgumentCaptor.

Drop me your questions related to invoking a method multiple times and verify it’s method different arguments values in mockito based unit tests.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Mockito – Verify multiple method calls with different arguments”

  1. hi. I am writing a test case for a class which like this:
    public class A {
    public void methodOne(int argument) {
    //some operations
    methodTwo(argument);
    //some operations
    }

    private void methodTwo(int argument) {
    ReportClass report = new ReportClass ();
    //use dateTime to perform some operations
    }
    }

    in this case i am not allowed to perform any change in src code but I need to set some attributes to ReportClass. beacuse of this class test case getting failed. I don’t know how to write a test case for this class.

    Reply
  2. Hi Lokesh,

    I have a class which must be tested with Mockito. De class has two dependent worker classes. Both worker classes are mocked in the test. But each of the two worker classes has the same argument type. How can I capture the two invocations of the two worker classes and check if the worker classes are called with the proper arguments ?

    Kind regards,
    Bart

    Reply
  3. Hi Lokesh,
    I want to put some static values in either array or hash map and compare them with logs in a CSV document to verify whether those values are being passed. what is the best of doing it
    For ex: CSV contains a value like in JSON request and I want to make sure this value is passed whenever the app is run

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.