w33z33
w33z33

Reputation: 378

Mockito Matcher for Integer... parameter

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

Answers (2)

Nicolas Filotto
Nicolas Filotto

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

Mureinik
Mureinik

Reputation: 311113

Integer... is a syntactic sugar on top of defining an array of Integers. So the correct way to mock it would be:

when(findSomething(any(), any(Integer[].class))).thenReturn(listOfSomething);

Upvotes: 2

Related Questions