HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Mockito / Mockito – Verify multiple method calls with different arguments

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 !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Supriya Chougule

    May 21, 2020

    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.

  2. Wayne

    April 15, 2020

    I want one method to return different result after some time eg. 1s. This is for rate limit test. How to mock it?

  3. Bart

    January 3, 2020

    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

  4. Sydney

    July 25, 2019

    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

Comments are closed on this article!

Search Tutorials

Mockito Tutorial

  • Mockito – Introduction
  • Mockito – Annotations
  • Mockito – @Mock vs @InjectMocks
  • Mockito – Multiple method calls
  • Mockito – Spring Boot
  • Mockito – MockMaker Error

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)