英文:
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.
英文:
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.
答案1
得分: 2
你可以尝试类似这样的代码:ArgumentMatchers.<List<Pair<Foo, Bar>>>any()
英文:
You could try something like this: ArgumentMatchers.<List<Pair<Foo, Bar>>>any()
答案2
得分: 0
你可以将你的参数匹配器存储在具有期望类型的变量中,从而解决模糊性。
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论