Mockito 区分模糊的 any() 集合与列表

huangapple go评论79阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2020年8月13日 09:40:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63386923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定