|
1 | 1 | package org.baeldung.mockito; |
2 | 2 |
|
3 | 3 | import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
4 | 5 | import static org.hamcrest.Matchers.is; |
5 | 6 | import static org.junit.Assert.assertThat; |
| 7 | +import static org.mockito.Matchers.anyInt; |
6 | 8 | import static org.mockito.Matchers.anyString; |
| 9 | +import static org.mockito.Mockito.doAnswer; |
| 10 | +import static org.mockito.Mockito.doReturn; |
7 | 11 | import static org.mockito.Mockito.doThrow; |
8 | 12 | import static org.mockito.Mockito.when; |
9 | 13 |
|
10 | 14 | import org.junit.Test; |
11 | 15 | import org.mockito.Mockito; |
| 16 | +import org.mockito.invocation.InvocationOnMock; |
| 17 | +import org.mockito.stubbing.Answer; |
12 | 18 |
|
13 | | -@SuppressWarnings("unchecked") |
14 | 19 | public class MockitoConfigExamplesTest { |
15 | 20 |
|
16 | 21 | // tests |
17 | 22 |
|
18 | 23 | @Test |
19 | | - public final void whenMockBehaviorIsConfigured_thenBehaviorIsVerified() { |
| 24 | + public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() { |
20 | 25 | final MyList listMock = Mockito.mock(MyList.class); |
21 | 26 | when(listMock.add(anyString())).thenReturn(false); |
22 | 27 |
|
23 | 28 | final boolean added = listMock.add(randomAlphabetic(6)); |
24 | 29 | assertThat(added, is(false)); |
25 | 30 | } |
26 | 31 |
|
| 32 | + @Test |
| 33 | + public final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() { |
| 34 | + final MyList listMock = Mockito.mock(MyList.class); |
| 35 | + doReturn(false).when(listMock).add(anyString()); |
| 36 | + |
| 37 | + final boolean added = listMock.add(randomAlphabetic(6)); |
| 38 | + assertThat(added, is(false)); |
| 39 | + } |
| 40 | + |
27 | 41 | @Test(expected = IllegalStateException.class) |
28 | 42 | public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() { |
29 | 43 | final MyList listMock = Mockito.mock(MyList.class); |
@@ -57,4 +71,35 @@ public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCall |
57 | 71 | listMock.add(randomAlphabetic(6)); |
58 | 72 | } |
59 | 73 |
|
| 74 | + @Test |
| 75 | + public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMetehodIsCalled() { |
| 76 | + final MyList listMock = Mockito.mock(MyList.class); |
| 77 | + when(listMock.size()).thenCallRealMethod(); |
| 78 | + |
| 79 | + assertThat(listMock.size(), equalTo(1)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMetehodIsCalled() { |
| 84 | + final MyList listMock = Mockito.mock(MyList.class); |
| 85 | + doAnswer(new Answer<String>() { |
| 86 | + @Override |
| 87 | + public final String answer(final InvocationOnMock invocation) { |
| 88 | + return "Always the same"; |
| 89 | + } |
| 90 | + }).when(listMock).get(anyInt()); |
| 91 | + |
| 92 | + final String element = listMock.get(1); |
| 93 | + assertThat(element, is(equalTo("Always the same"))); |
| 94 | + } |
| 95 | + |
| 96 | + @Test(expected = NullPointerException.class) |
| 97 | + public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() { |
| 98 | + final MyList instance = new MyList(); |
| 99 | + final MyList spy = Mockito.spy(instance); |
| 100 | + |
| 101 | + doThrow(NullPointerException.class).when(spy).size(); |
| 102 | + spy.size(); |
| 103 | + } |
| 104 | + |
60 | 105 | } |
0 commit comments