Mockito
Java
varargs
unit testing
mocking

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 (...):

java
public void someMethod(String... args) {
    // method implementation
}

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:

java
1import static org.mockito.Mockito.*;
2
3// Assume ListInterface is a mock of some interface with varargs method.
4ListInterface mockList = mock(ListInterface.class);
5
6// this method accepts varargs
7mockList.doSomething("a", "b");
8
9// verify that doSomething was called with any varargs
10verify(mockList).doSomething(any(String[].class));

One matcher per argument with eq()

Use eq() for each argument when order and content matter:

java
1import static org.mockito.Mockito.*;
2
3ListInterface mockList = mock(ListInterface.class);
4
5// mock interaction with specific arguments
6mockList.doSomething("arg1", "arg2");
7
8// verify using specific matchers
9verify(mockList).doSomething(eq("arg1"), eq("arg2"));

Mixing exact and wildcard matchers

You can combine exact matchers with anyString() for the positions you do not care about:

java
1import static org.mockito.Mockito.*;
2
3ListInterface mockList = mock(ListInterface.class);
4
5mockList.doSomething("arg1", "arg2", "arg3");
6
7// verify with combination of matchers
8verify(mockList).doSomething(eq("arg1"), anyString(), eq("arg3"));

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:

java
1import static org.mockito.ArgumentMatchers.*;
2import static org.mockito.Mockito.*;
3
4import org.mockito.ArgumentMatcher;
5
6ListInterface mockList = mock(ListInterface.class);
7
8mockList.process("arg1", "arg2");
9
10verify(mockList).process(argThat(new ArgumentMatcher<String[]>() {
11    @Override
12    public boolean matches(String[] args) {
13        return args.length == 2;
14    }
15
16    @Override
17    public Class<?> type() {
18        return String[].class;
19    }
20}));

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:

java
1import static org.mockito.Mockito.*;
2
3ListInterface mockList = mock(ListInterface.class);
4
5mockList.doSomething((String[]) null);
6
7verify(mockList).doSomething((String[]) isNull());

Quick reference

PointDescription
General MatchingUse any() to match varargs of any content.
Specific MatchingUse eq() or other specific matchers for each argument.
Combining MatchersCombine any(), eq(), etc., to tailor matches.
Handling NullsUse (String[]) isNull() to match a null varargs array.
Counting VarargsUse 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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms