Mockito

Mockito is a Java mocking framework for unit tests.

Creating a mock

import static org.mockito.Mockito.*;
// mock creation
List mockedList = mock(List.class);
// Or @Mock List mockedList;

Verifying interactions

mockedList.add("one");
verify(mockedList).add("one");
verify(mockedList).add("two"); // will fail because we never called with this value

Stubbing return values

when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());

Argument captor

verify(mockStorage).barcode(argCaptor.capture());
// this line verifies the barcode function is called and captures its input
verify(mockDisplay).showLine(argCaptor.getValue());
// ensures the same value is passed to both barcode and showLine