如果您只想模拟函数参数,那么以下任一项都可以:
Mockito.when(convertStringtoInt(Matchers.anyList(), Mockito.any(Function.class))).thenReturn(myMockedList);
Mockito.when(convertStringtoInt(Matchers.anyList(), Mockito.<Function>anyObject())).thenReturn(myMockedList);
给定一个类,
Foo
,其中包含方法:
public List<String> convertStringtoInt(List<Integer> intList,Function<Integer, String> intToStringExpression)
以下测试用例通过:
@Test
public void test_withMatcher() {
Foo foo = Mockito.mock(Foo.class);
List<String> myMockedList = Lists.newArrayList("a", "b", "c");
Mockito.when(foo.convertStringtoInt(Matchers.anyList(), Mockito.<Function>anyObject())).thenReturn(myMockedList);
List<String> actual = foo.convertStringtoInt(Lists.newArrayList(1), new Function<Integer, String>() {
@Override
public String apply(Integer integer) {
return null;
}
});
assertEquals(myMockedList, actual);
}
thenAnswer()
.