GenTel
GenTel

Reputation: 1558

Mockito Distinguish Ambiguous any() Collection vs List

I am trying to mock an overloaded method that takes the following arguments:

Collection<Foo>

List<Pair<Foo, Bar>>

My problem is I want to do Mockito.when to get the list-based method. But if I do Mockito.anyList then that's still ambiguous because that's still a collection.

I have tried doing Mockito.any(List.class) but that's also ambiguous, and when I try Mockito.any(List<Pair>.class) I can't get it from the parameterized type.

What can I do to distinguish them? Mockito.listOf seemed promising but hasn't worked thus far.

Upvotes: 1

Views: 1235

Answers (2)

Vladimir Stanciu
Vladimir Stanciu

Reputation: 1536

You could try something like this: ArgumentMatchers.<List<Pair<Foo, Bar>>>any()

Upvotes: 3

Lesiak
Lesiak

Reputation: 26006

You can hold your argument matcher in a variable with expected type, and thus resolve the ambiguity.

List<Pair<Foo, Bar>> listMatcher = ArgumentMatchers.anyList();
Mockito.when(myVar.doSth(listMatcher)).thenReturn(someValue);

Upvotes: 0

Related Questions