chapter 1 - 49 doc link
1.Let's verify some behaviour!
一下例子mock 了一个 List ,因为List 的method 大家较为熟知所以方便理解, 现实测试中 不要用 mock List
import static org.mockito.Mockito*;
// mock creation
List mockedList = mock(List.class);
// using mock object
mockedList.add("one");
mockedList.clear();
// verification
verify(mockedList).add("one");
verify(mockedList).clear();
一旦mock 生成以后, mock 会记住所有interactions。之后我们可以有选择性的.
2. how about some stubbing
我们既可以 mock interface 例如 List , 我也可以mock concrete class 例如 LinkedList.
// You can mock concrete classes, not just interfaces
LinkedList mockedList = mock(List.class);
// stubbing
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow( new RuntimeException());
//following prints "first"
System.out.println( mockedList.get(0) );
//following throws runtime exception
System.out.println( mockedList.get(1) );
//following prints "null" because get(999) was not stubbed
System.out.println( mockedList.get(999) );
verify(mockedList).get(0);
通常情况下, mock object 会return null ,a primitive/primitive wrapper value, or an empty collection, as appropriate. For example 0 for an int/Integer and false for a boolean/Boolean.
一旦stubbed, 这个method 总会 return stubbed value , 无论call 这个method 多少次都是一样。
Last stubbing is more important - when you stubbed the same method with the same arguments many times. Other words: the order of stubbing matters but it is only meaningful rarely, e.g. when stubbing exactly the same method calls or sometimes when argument matchers are used, etc.
3. Argument matchers 参数匹配器
mockito 会verify 参数值
// stubbing using built-in anyInt() argument matcher
when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
when(mockedList.contains(argThat(isValid()))).thenReturn(true);
//following prints "element"
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
verify(mockedList).get(anyInt());
Argument matchers allow flexible verification or stubbing. Click here
or here
to see more built-in matchers and examples of custom argument matchers / hamcrest matchers.
注意: 如果用了argument matchers 作为 method 的参数, 就必须所有参数都用 argument matchers .
verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));
//above is correct - eq() is also an argument matcher
verify(mock).someMethod(anyInt(), anyString(), "third argument");
//above is incorrect - exception will be thrown because third argument is given without an argument matcher.
4. Verifying exact number of invocations
verify 一个method 被call 了几次
//using mock
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
//following two verifications work exactly the same - times(1) is used by default
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
//exact number of invocations verification
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
//verification using never(). never() is an alias to times(0)
verify(mockedList, never()).add("never happened");
//verification using atLeast()/atMost()
verify(mockedList, atMostOnce()).add("once");
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atMost(5)).add("three times");
time(1) 是默认的, 因此time(1) 是默认的。
5. Stubbing void methods with exceptions