问题描述
我正在尝试模拟采用以下参数的重载方法:
Collection<Foo>
List<Pair<Foo,Bar>>
我的问题是我想做Mockito.when
以获取基于列表的方法。但是如果我做Mockito.anyList
,那仍然是模棱两可的,因为那仍然是一个集合。
我尝试做Mockito.any(List.class)
,但这也模棱两可,当我尝试Mockito.any(List<Pair>.class)
时,无法从参数化类型中获取它。
我该如何区分它们? Mockito.listof
看起来很有希望,但到目前为止还没有奏效。
解决方法
您可以尝试以下操作:ArgumentMatchers.<List<Pair<Foo,Bar>>>any()
您可以将参数匹配器保存在具有预期类型的变量中,从而解决歧义。
List<Pair<Foo,Bar>> listMatcher = ArgumentMatchers.anyList();
Mockito.when(myVar.doSth(listMatcher)).thenReturn(someValue);