我是 Android 测试新手,并决定从http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#3中的 Mockito 示例开始
为了完全理解发生了什么,我决定从示例中分支出来,这是我的代码:
@Test
public void testMatchers() throws Exception
{
LinkedList <String> mockedList = mock (LinkedList.class);
when (mockedList.contains(argThat(isValidStr()))).thenReturn(true);
System.out.println(mockedList.contains("Asdf")); // prints 'true', as expected
System.out.println(mockedList.contains("asdf")); // prints 'true' which I guess makes sense... but shouldn't the argument matcher complain somehow?
}
public static ArgumentMatcher<String> isValidStr()
{
return new ArgumentMatcher<String> ()
{
@Override
public boolean matches ( Object argument )
{
String str = (String) argument;
return (str.charAt(0) > 'A' || str.charAt(0) < 'Z' ) // if first letter is capitalized
}
}
}
所有测试都通过了,有 0 个异常/错误。另外,我在“匹配”函数的开头放置了一个断点,它永远不会被命中。
3 个问题:1)为什么我的论点匹配器不抱怨一个糟糕的论点?
2) 检测到错误参数后的预期输出是什么?3) 检测到错误参数的代码看起来如何?