[JAVA] Verify the contents of the argument object with Mockito

When using Mockito, it is often said that "whether a method is called" is verified using the verify times (), but the argument (contents of the object) passed to the method is verified. Is there a way to do it? I thought and examined it.


https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/ArgumentMatchers.html https://www.codeflow.site/ja/article/mockito-argument-matchers

How to make a matcher

Create a custom Matcher by inheriting from org.mockito.ArgumentMatcher. HashMap would look like this.


import java.util.HashMap;

import org.mockito.ArgumentMatcher;

public class HashMapMatcher implements ArgumentMatcher<HashMap<String, String>> {
	HashMap<String, String> map;

    public HashMapMatcher(HashMap<String, String> map) {
        this.map = map;

    public boolean matches(HashMap<String, String> actual) {
		if (this.map.keySet().size() != actual.keySet().size()) {
    		return false;
    	for (String key : this.map.keySet()) {
    		if (!actual.containsKey(key)) {
    			return false;
    		if (!this.map.get(key).equals(actual.get(key))) {
    			return false;
        return true;
    //Override and get all the contents of the map out will make it easier if the test fails.
    public String toString() {
    	//You can choose how you want to output it.
    	StringBuffer sb = new StringBuffer();
    	for (String key : this.map.keySet()) {
    		sb.append(", value:").append(map.get(key)).append("]");
    	return sb.toString();


How to use

Pass a custom Matcher to org.mockito.ArgumentMatchers.argThat and use it.


// hoge.save(map);Is called to verify that its arguments are the same as the expected value
verify(hoge,atLeastOnce()).save(argThat(new HashMapMatcher(map)));

At the end

It seems that it can be used not for verify but for verification at when ~ then Return. Let's enjoy test life.

Recommended Posts

Verify the contents of the argument object with Mockito
[Rails] Check the contents of the object
Check the contents of params with pry
Format the contents of LocalDate with DateTimeFormatter
Overwrite the contents of config with Spring-boot + JUnit5
List the contents of categories created with Active Hash
Test the contents of an Excel file with JUnit
The illusion of object orientation
Replace the contents of the Jar file
[Ruby] Display the contents of variables
Check the contents of the Java certificate store
Memo: [Java] Check the contents of the directory
Folding and unfolding the contents of the Recyclerview
[Ruby] Cut off the contents of twitter-ads
Verify the uniqueness of your email address
Check the processing contents with [rails] binding.pry
About the treatment of BigDecimal (with reflection)
The contents of the data saved by CarrierWave.
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
After all I wanted to preview the contents of mysql with Docker ...
Increment with the third argument of iterate method of Stream class added from Java9
JAVA: jar, aar, view the contents of the file
Customize how to divide the contents of Recyclerview
Manage the version of Ruby itself with rbenv
The story of tuning android apps with libGDX
I want to var_dump the contents of the intent
[Ruby] "Reference to object" and "Contents of variable"
Calculate the similarity score of strings with JAVA
Prepare the environment of CentOS 8 with Sakura VPS
Specify the default value with @Builder of Lombok
Measure the distance of the maze with breadth-first search
I checked the number of taxis with Ruby
[Docker] How to see the contents of Volumes. Start a container with root privileges.
[Swift] Get the number of steps with CMP edometer
[Kotlin] Get the argument name of the constructor by reflection
JavaFX --Match the size of ImageView with other nodes
CI the architecture of Java / Kotlin applications with ArchUnit
[JUnit5] Dealing with "the reference of assertEquals is ambiguous"
Access the built-in h2db of spring boot with jdbcTemplate
The story of making a reverse proxy with ProxyServlet
Monitor the internal state of Java programs with Kubernetes
Implement the UICollectionView of iOS14 with the minimum required code.
Check the behavior of Java Intrinsic Locks with bpftrace
[Rails] How to get the contents of strong parameters
Check the result of generic parameter inference with JShell
Java: Use Stream to sort the contents of the collection
Roughly the flow of web application development with Rails.
Control the processing flow of Spring Batch with JavaConfig.
How is the next value of the Time object correct?
I want to be aware of the contents of variables!
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
I want to recreate the contents of assets from scratch in the environment built with capistrano