参数匹配器
Argument Matcher(参数匹配器)
Mockito通过equals()方法,来对方法参数进行验证。但是有时候我们需要更加灵活的参数需求,比如,匹配任何的String类型的参数等等。参数匹配器就是一个能够满足这些需求的工具。
Mockito框架中的Matchers类内建了很多参数匹配器,我们常用的Mockito对象就是继承自Matchers。比如anyInt()匹配任何int类型的参数,anyString()匹配任何字符串...
@Test
public void argumentMatchersTest(){
List mock = mock(List.class);
when(mock.get(anyInt())).thenReturn("Hello").thenReturn("World");
String result=mock.get(100)+" "+mock.get(200);
verify(mock,times(2)).get(anyInt());
assertEquals("Hello World",result);
}