User:Rettamkrad/sandbox

Mock
//You can mock concrete classes, not only interfaces LinkedList mockedList = mock(LinkedList.class);

Stubbing void methods
//stubbing when(mockedList.get(0)).thenReturn("first"); when(mockedList.get(1)).thenThrow(new RuntimeException);

Verify
//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)); //Although it is possible to verify a stubbed invocation, usually it's just redundant //If your code cares what get(0) returns then something else breaks (often before even verify gets executed). //If your code doesn't care what get(0) returns then it should not be stubbed. Not convinced? See here. verify(mockedList).get(0);

Note
What are the limitations of Mockito

Needs java 1.5+ Cannot mock final classes Cannot mock static methods Cannot mock final methods - their real behavior is executed without any exception. Mockito cannot warn you about mocking final methods so be vigilant. Cannot mock private methods Cannot mock equals, hashCode. Firstly, you should not mock those methods. Secondly, Mockito defines and depends upon a specific implementation of these methods. Redefining them might break Mockito.