Friday, 1 April 2016


 1 : verify 
 //Let's import Mockito statically so that the code looks clearer
 import static org.mockito.Mockito.*;
 //mock creation
 List mockedList = mock(List.class);
 //using mock object
 2 :  Argument matchers
 //stubbing using built-in anyInt() argument matcher
 //stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher):
 //following prints "element"
 //you can also verify using an argument matcher
 Argument matchers allow flexible verification or stubbing.
 3 : Verifying exact number of invocations / at least x / never
 //using mock 
 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, 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, atLeastOnce()).add("three times");
 verify(mockedList, atLeast(2)).add("five times");
 verify(mockedList, atMost(5)).add("three times");