diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java index 7861741a79..399c511d4e 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoConfigExamplesTest.java @@ -1,22 +1,27 @@ package org.baeldung.mockito; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import org.junit.Test; import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; -@SuppressWarnings("unchecked") public class MockitoConfigExamplesTest { // tests @Test - public final void whenMockBehaviorIsConfigured_thenBehaviorIsVerified() { + public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() { final MyList listMock = Mockito.mock(MyList.class); when(listMock.add(anyString())).thenReturn(false); @@ -24,6 +29,15 @@ public class MockitoConfigExamplesTest { assertThat(added, is(false)); } + @Test + public final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() { + final MyList listMock = Mockito.mock(MyList.class); + doReturn(false).when(listMock).add(anyString()); + + final boolean added = listMock.add(randomAlphabetic(6)); + assertThat(added, is(false)); + } + @Test(expected = IllegalStateException.class) public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() { final MyList listMock = Mockito.mock(MyList.class); @@ -57,4 +71,35 @@ public class MockitoConfigExamplesTest { listMock.add(randomAlphabetic(6)); } + @Test + public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMetehodIsCalled() { + final MyList listMock = Mockito.mock(MyList.class); + when(listMock.size()).thenCallRealMethod(); + + assertThat(listMock.size(), equalTo(1)); + } + + @Test + public final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMetehodIsCalled() { + final MyList listMock = Mockito.mock(MyList.class); + doAnswer(new Answer() { + @Override + public final String answer(final InvocationOnMock invocation) { + return "Always the same"; + } + }).when(listMock).get(anyInt()); + + final String element = listMock.get(1); + assertThat(element, is(equalTo("Always the same"))); + } + + @Test(expected = NullPointerException.class) + public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() { + final MyList instance = new MyList(); + final MyList spy = Mockito.spy(instance); + + doThrow(NullPointerException.class).when(spy).size(); + spy.size(); + } + } diff --git a/mockito/src/test/java/org/baeldung/mockito/MyList.java b/mockito/src/test/java/org/baeldung/mockito/MyList.java index 23a8f65e6c..548596e6b6 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MyList.java +++ b/mockito/src/test/java/org/baeldung/mockito/MyList.java @@ -4,14 +4,14 @@ import java.util.AbstractList; public class MyList extends AbstractList { - @Override - public String get(final int index) { - return null; - } + @Override + public String get(final int index) { + return null; + } - @Override - public int size() { - return 0; - } + @Override + public int size() { + return 1; + } }