194

I've been trying to get to mock a method with vararg parameters using Mockito:

interface A {
  B b(int x, int y, C... c);
}

A a = mock(A.class);
B b = mock(B.class);

when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b);
assertEquals(b, a.b(1, 2));

This doesn't work, however if I do this instead:

when(a.b(anyInt(), anyInt())).thenReturn(b);
assertEquals(b, a.b(1, 2));

This works, despite that I have completely omitted the varargs argument when stubbing the method.

Any clues?

qualidafial
  • 6,674
  • 3
  • 28
  • 38
  • the fact that last example works is rather trivial since it matches the case when zero varargs parameters passed. – topchef Apr 14 '10 at 02:42

12 Answers12

275

Mockito 1.8.1 introduced anyVararg() matcher:

when(a.b(anyInt(), anyInt(), Matchers.<String>anyVararg())).thenReturn(b);

Also see history for this: https://code.google.com/archive/p/mockito/issues/62

Edit new syntax after deprecation:

when(a.b(anyInt(), anyInt(), ArgumentMatchers.<String>any())).thenReturn(b);
pdem
  • 3,880
  • 1
  • 24
  • 38
topchef
  • 19,091
  • 9
  • 63
  • 102
  • 29
    `anyVararg()` has Object as its return type. To make it compatible with any var arg types (e.g. String ..., Integer ..., etc.), do an explicit casting. For example, if you have `doSomething(Integer number, String ... args)` you can do the mock/stub code with something like `when(mock).doSomething(eq(1), (String) anyVarargs())`. That should take care of the compilation error. – Psycho Punch Dec 11 '12 at 07:55
  • 21
    for info anyVararg is now deprecated: "@deprecated as of 2.1.0 use any()" – alexbt Nov 13 '16 at 20:12
  • 5
    `Matchers` is now deprecated in order to avoid a name clash with `org.hamcrest.Matchers` class and will likely be removed in mockito v3.0. Use `ArgumentMatchers` instead. – JonyD Jul 13 '17 at 12:54
  • 4
    `anyVararg()` has been deprecated in `ArgumentMatchers` as well. – SQB Jul 05 '21 at 07:52
  • @SQB thanks! So what's the replacement/alternative? – JohnK Apr 15 '22 at 16:41
  • 2
    N.B. If you have an ambiguous method call problem (e.g. there is one method that takes a single argument and another method that takes varargs), you can fix the problem by casting the `any` method to a typed array. For instance: `(String[]) any()`. – Jacob van Lingen Sep 29 '22 at 14:31
  • `ArgumentMatchers.any() ` did not work for me on Mockito 3.3 but `(String[]) any()` did. – Konstantin Rezchikov Apr 04 '23 at 20:38
33

A somewhat undocumented feature: If you want to develop a custom Matcher that matches vararg arguments you need to have it implement org.mockito.internal.matchers.VarargMatcher for it to work correctly. It's an empty marker interface, without which Mockito will not correctly compare arguments when invoking a method with varargs using your Matcher.

For example:

class MyVarargMatcher extends ArgumentMatcher<C[]> implements VarargMatcher {
    @Override public boolean matches(Object varargArgument) {
        return /* does it match? */ true;
    }
}

when(a.b(anyInt(), anyInt(), argThat(new MyVarargMatcher()))).thenReturn(b);
Eli Levine
  • 431
  • 4
  • 2
9

Building on Eli Levine's answer here is a more generic solution:

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.mockito.ArgumentMatcher;
import org.mockito.internal.matchers.VarargMatcher;

import static org.mockito.Matchers.argThat;

public class VarArgMatcher<T> extends ArgumentMatcher<T[]> implements VarargMatcher {

    public static <T> T[] varArgThat(Matcher<T[]> hamcrestMatcher) {
        argThat(new VarArgMatcher(hamcrestMatcher));
        return null;
    }

    private final Matcher<T[]> hamcrestMatcher;

    private VarArgMatcher(Matcher<T[]> hamcrestMatcher) {
        this.hamcrestMatcher = hamcrestMatcher;
    }

    @Override
    public boolean matches(Object o) {
        return hamcrestMatcher.matches(o);
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("has varargs: ").appendDescriptionOf(hamcrestMatcher);
    }

}

Then you can use it with hamcrest's array matchers thus:

verify(a).b(VarArgMatcher.varArgThat(
            org.hamcrest.collection.IsArrayContaining.hasItemInArray("Test")));

(Obviously static imports will render this more readable.)

Rachel K. Westmacott
  • 2,038
  • 20
  • 15
  • Nice. This should be built into Mockito IMO. – bryant Sep 22 '15 at 23:55
  • I filed an issue against Hamcrest to add something like this. See https://github.com/mockito/mockito/issues/356 – Mark Feb 15 '16 at 09:58
  • Is this for Mockito 1? I get various compilation errors when trying to compile against 2.10. – Frans Feb 15 '18 at 05:46
  • @Frans it looks like the 2 release was still in beta when I wrote this answer, so yes it was probably written for Mockito v1.10.19 or thereabouts. (https://github.com/mockito/mockito/releases) It is probably updatable... :-D – Rachel K. Westmacott Feb 20 '18 at 09:27
8

I have been using the code in Peter Westmacott's answer however with Mockito 2.2.15 you can now do the following:

verify(a).method(100L, arg1, arg2, arg3)

where arg1, arg2, arg3 are varargs.

Mark
  • 28,783
  • 8
  • 63
  • 92
3

Building on topchef's answer,

For 2.0.31-beta I had to use Mockito.anyVararg instead of Matchers.anyVararrg:

when(a.b(anyInt(), anyInt(), Mockito.<String>anyVararg())).thenReturn(b);
NPike
  • 13,136
  • 12
  • 63
  • 80
3

I had to use the any(Class type) method to match an array arg being passed as a varargs parameter.

ArgumentMatchers.any(Class type)

Code in the implementation is vararg aware.

reportMatcher(new InstanceOf.VarArgAware(

In my case where matching a String[] arg to a String... param the following worked:-

any(String.class)
mattocaramba
  • 121
  • 1
  • 3
1

Adapting the answer from @topchef,

Mockito.when(a.b(Mockito.anyInt(), Mockito.anyInt(), Mockito.any())).thenReturn(b);

Per the java docs for Mockito 2.23.4, Mockito.any() "Matches anything, including nulls and varargs."

Craig
  • 2,286
  • 3
  • 24
  • 37
1

You can accomplish this by passing an ArgumentCaptor capture and then retrieving the varargs as a list using "getAllValues", see: https://stackoverflow.com/a/55621731/11342928

Clarke Lacher
  • 79
  • 1
  • 2
  • (mockito-core = ver 4.5.1) It does work by "getAllValues()", although it is very weird. At compile time, the compiler thought "getAllValues()" is a "List". But at runtime, it is a "List" instead. – Anderson Aug 14 '22 at 07:52
1

As the other answers make sense and make tests work obviously, I still recommend to test as if the method didn't take a vararg, but rather regular well-defined parameters instead. This helps in situations where overridden methods in connection with possible ambiguous parameters are in place, like an SLF4J-logger:

to test:

jobLogger.info("{} finished: {} tasks processed with {} failures, took {}", jobName, count, errors, duration);

This has a bunch of overrides and the important method being declared like so

Logger.info(String, Object...)

verification:

verify(loggerMock).info(anyString(), anyString(), anyInt(), anyInt(), anyString());

proof that the above works as errors is an integer and not a long, so the following wouldn't run:

verify(loggerMock).info(anyString(), anyString(), anyInt(), anyLong(), anyString());

So you can easily use when() instead of the verify()-stuff to set up the required return value.

And it probably shows more of the intent and is more readable. Captures can also be used here and they are much easier accessible this way.

Tested with Mockito 2.15

sjngm
  • 12,423
  • 14
  • 84
  • 114
0

In my case the signature of the method that I want to capture its argument is:

    public byte[] write(byte ... data) throws IOException;

In this case you should cast to byte array explicitly:

       when(spi.write((byte[])anyVararg())).thenReturn(someValue);

I'm using mockito version 1.10.19

artronics
  • 1,399
  • 2
  • 19
  • 28
0

You can also loop over the arguments:

Object[] args = invocation.getArguments(); 
for( int argNo = 0; argNo < args.length; ++argNo) { 
    // ... do something with args[argNo] 
}

for example check their types and cast them appropriately, add to a list or whatever.

0

I've looked up for various ways but most of the ways are not working as described. What worked for me is.

Method Signature:

ReturnType someMethod(String... strings)

Mockito to handle above method:

when(someMethod((String[])Mockito.any())).thenReturn(dummyReturnTypeObject);

mocking a method with varargs

if your mock is successful then the value that appears for the mocked step wouldn't be null

result of mocking a method with varargs

Andrei Titov
  • 1,220
  • 2
  • 15
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '23 at 20:59