How to properly match varargs in Mockito
Codemia Knowledge Hub ·
To match a varargs parameter in Mockito, either match the whole argument with any(String[].class), or give one matcher per argument, such as eq("arg1") or anyString(). Use argThat() when you need to check how many arguments were passed.
What a varargs method looks like
Varargs (variable-length argument lists) let a method accept zero or more arguments of a type. In Java you declare one with an ellipsis (...):
Matching these arguments correctly matters when you verify interactions or stub behavior.
Matching any varargs with any()
The any() matcher accepts any argument of the given type:
One matcher per argument with eq()
Use eq() for each argument when order and content matter:
Mixing exact and wildcard matchers
You can combine exact matchers with anyString() for the positions you do not care about:
Checking the number of arguments with argThat()
Matching an exact count is harder, especially when some arguments are optional. Use argThat() with a condition on the length. In Mockito 5 the matcher must report the array type through type(), otherwise Mockito applies it to each element and a plain lambda never matches the whole array:
Matching a null varargs array
Null varargs can be ambiguous, because varargs default to an empty array when none are provided. You can explicitly verify a call with a null array:
Quick reference
| Point | Description |
| General Matching | Use any() to match varargs of any content. |
| Specific Matching | Use eq() or other specific matchers for each argument. |
| Combining Matchers | Combine any(), eq(), etc., to tailor matches. |
| Handling Nulls | Use (String[]) isNull() to match a null varargs array. |
| Counting Varargs | Use argThat() to specify conditions on the number of arguments. |
In short: use any() when the content does not matter to the test, eq() when it does, mix them to pin down only the arguments you care about, check the count with argThat() when it is important, and handle null varargs deliberately.
Related reading
- how to properly specify database schema in spring boot?
- How to properly stop the Thread in Java?
- How to proxy a HTTP video stream to any amount of clients through a Spring Webserver
- How to quickly and conveniently create a one element arraylist
- How to properly unit test a .NET project with multiple target frameworks, given implementation differences among targets?
- How to properly use unit-testing's assertRaises with NoneType objects
- How to randomize two ArrayLists in the same fashion?
- How to re-create database before each test in Spring?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.