import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.exceptions.verification.NoInteractionsWanted;
import java.util.LinkedList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.*;
@SuppressWarnings("unchecked")
public class _01_Mock {
@Test
public void mock() {
List<String> mockedList = mock(List.class);
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
}
@Test
public void stubbing() {
LinkedList mockedList = mock(LinkedList.class);
when(mockedList.get(0)).thenReturn("first1", "first2");
when(mockedList.get(1)).thenThrow(new RuntimeException("RuntimeException"));
assertEquals(mockedList.get(0), "first1");
assertEquals(mockedList.get(0), "first2");
assertEquals(mockedList.get(0), "first2");
try {
mockedList.get(1);
assertFalse(true);
} catch (RuntimeException e) {
assertEquals(e.getMessage(), "RuntimeException");
}
assertEquals(mockedList.get(999), null);
verify(mockedList, times(3)).get(0);
}
@Test
public void verifyingExactNumberOfInvocations() {
LinkedList mockedList = mock(LinkedList.class);
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
verify(mockedList, never()).add("never happened");
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atMost(5)).add("three times");
}
@Test(expected = RuntimeException.class)
public void stubbingVoidMethodsWithExceptions() {
LinkedList mockedList = mock(LinkedList.class);
doThrow(new RuntimeException()).when(mockedList).clear();
mockedList.clear();
}
@Test
public void verificationInOrder() {
List singleMock = mock(List.class);
singleMock.add("was added first");
singleMock.add("was added second");
InOrder inOrder = inOrder(singleMock);
inOrder.verify(singleMock).add("was added first");
inOrder.verify(singleMock).add("was added second");
List firstMock = mock(List.class);
List secondMock = mock(List.class);
firstMock.add("was called first");
secondMock.add("was called second");
InOrder inOrder2 = inOrder(firstMock, secondMock);
inOrder2.verify(firstMock).add("was called first");
inOrder2.verify(secondMock).add("was called second");
}
@Test
public void makingSureInteractionsNeverHappenedOnMock() {
List mockOne = mock(List.class);
List mockTwo = mock(List.class);
List mockThree = mock(List.class);
mockOne.add("one");
verify(mockOne).add("one");
verify(mockOne, never()).add("two");
verifyZeroInteractions(mockTwo, mockThree);
}
@Test(expected = NoInteractionsWanted.class)
public void findingRedundantInvocations() {
LinkedList mockedList = mock(LinkedList.class);
mockedList.add("one");
mockedList.add("two");
verify(mockedList).add("one");
verifyNoMoreInteractions(mockedList);
}
}