Reputation: 378
What's the proper Mockito matcher for the second parameter in this method Signature:
List<Something> findSomething(Object o, Integer... ids);
I tried the following matchers:
when(findSomething(any(), anyInt())).thenReturn(listOfSomething);
when(findSomething(any(), any())).thenReturn(listOfSomething);
but Mockito is not creating the proxy for me, the returned List
is empty.
Upvotes: 3
Views: 3587
Reputation: 44965
Use anyVararg()
like this:
Application application = mock(Application.class);
List<Application> aps = Collections.singletonList(new Application());
when(application.findSomething(any(), anyVararg())).thenReturn(aps);
System.out.println(application.findSomething("foo").size());
System.out.println(application.findSomething("bar", 17).size());
System.out.println(application.findSomething(new Object(), 17, 18, 19, 20).size());
Output:
1
1
1
Upvotes: 5
Reputation: 311113
Integer...
is a syntactic sugar on top of defining an array of Integer
s. So the correct way to mock it would be:
when(findSomething(any(), any(Integer[].class))).thenReturn(listOfSomething);
Upvotes: 2