|
1 | 1 | package com.baeldung.gettersetter;
|
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 5 | +import static org.mockito.ArgumentMatchers.anyString; |
| 6 | +import static org.mockito.ArgumentMatchers.eq; |
3 | 7 | import static org.mockito.Mockito.doAnswer;
|
4 | 8 | import static org.mockito.Mockito.doNothing;
|
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.verify; |
5 | 11 | import static org.mockito.Mockito.when;
|
6 | 12 |
|
7 | 13 | import org.junit.Test;
|
8 |
| -import org.junit.jupiter.api.Assertions; |
9 |
| -import org.mockito.ArgumentCaptor; |
10 |
| -import org.mockito.ArgumentMatchers; |
11 |
| -import org.mockito.Mockito; |
12 |
| -import org.mockito.invocation.InvocationOnMock; |
13 | 14 | import org.mockito.stubbing.Answer;
|
14 | 15 |
|
15 |
| -import net.bytebuddy.asm.Advice; |
16 |
| - |
17 | 16 | public class ExampleServiceTest {
|
18 | 17 |
|
19 |
| - private final ExampleService testee = new ExampleService(); |
20 |
| - |
21 | 18 | @Test
|
22 |
| - public void givenSimpleClass_whenInvokingGetId_thenReturnId() { |
23 |
| - SimpleClass simple = new SimpleClass(1L, "Jack"); |
24 |
| - Assertions.assertEquals(testee.getId(simple), simple.getId()); |
| 19 | + public void givenMockedSimpleClass_whenInvokingSettersGetters_thenInvokeMockedSettersGetters() { |
| 20 | + Long mockId = 12L; |
| 21 | + String mockName = "I'm 12"; |
| 22 | + SimpleClass simpleMock = mock(SimpleClass.class); |
| 23 | + when(simpleMock.getId()).thenReturn(mockId); |
| 24 | + when(simpleMock.getName()).thenReturn(mockName); |
| 25 | + doNothing().when(simpleMock) |
| 26 | + .setId(anyLong()); |
| 27 | + doNothing().when(simpleMock) |
| 28 | + .setName(anyString()); |
| 29 | + ExampleService srv = new ExampleService(); |
| 30 | + srv.setField(simpleMock::setId, 11L); |
| 31 | + srv.setField(simpleMock::setName, "I'm 11"); |
| 32 | + assertEquals(srv.getField(simpleMock::getId), mockId); |
| 33 | + assertEquals(srv.getField(simpleMock::getName), mockName); |
| 34 | + verify(simpleMock).getId(); |
| 35 | + verify(simpleMock).getName(); |
| 36 | + verify(simpleMock).setId(eq(11L)); |
| 37 | + verify(simpleMock).setName(eq("I'm 11")); |
25 | 38 | }
|
26 | 39 |
|
27 | 40 | @Test
|
28 |
| - public void givenSimpleClass_whenInvokingGetName_thenReturnName() { |
29 |
| - SimpleClass simple = new SimpleClass(1L, "Alex"); |
30 |
| - Assertions.assertEquals(testee.getName(simple), simple.getName()); |
| 41 | + public void givenActualSimpleClass_whenInvokingSettersGetters_thenInvokeActualSettersGetters() { |
| 42 | + Long id = 1L; |
| 43 | + String name = "I'm 1"; |
| 44 | + SimpleClass simple = new SimpleClass(id, name); |
| 45 | + ExampleService srv = new ExampleService(); |
| 46 | + srv.setField(simple::setId, 2L); |
| 47 | + srv.setField(simple::setName, "I'm 2"); |
| 48 | + assertEquals(srv.getField(simple::getId), simple.getId()); |
| 49 | + assertEquals(srv.getField(simple::getName), simple.getName()); |
31 | 50 | }
|
32 | 51 |
|
33 | 52 | @Test
|
34 | 53 | public void givenNonSimpleClass_whenInvokingGetName_thenReturnMockedName() {
|
35 |
| - NonSimpleClass nonSimple = Mockito.mock(NonSimpleClass.class); |
| 54 | + NonSimpleClass nonSimple = mock(NonSimpleClass.class); |
36 | 55 | when(nonSimple.getName()).thenReturn("Meredith");
|
37 |
| - Assertions.assertEquals(testee.getName(nonSimple), "Meredith"); |
| 56 | + ExampleService srv = new ExampleService(); |
| 57 | + assertEquals(srv.getField(nonSimple::getName), "Meredith"); |
| 58 | + verify(nonSimple).getName(); |
38 | 59 | }
|
39 | 60 |
|
40 | 61 | static class Wrapper<T> {
|
@@ -62,17 +83,18 @@ void set(T value) {
|
62 | 83 | @Test
|
63 | 84 | public void givenNonSimpleClass_whenInvokingGetName_thenReturnTheLatestNameSet() {
|
64 | 85 | Wrapper<String> nameWrapper = new Wrapper<>(String.class);
|
65 |
| - NonSimpleClass nonSimple = Mockito.mock(NonSimpleClass.class); |
| 86 | + NonSimpleClass nonSimple = mock(NonSimpleClass.class); |
66 | 87 | when(nonSimple.getName()).thenAnswer((Answer<String>) invocationOnMock -> nameWrapper.get());
|
67 | 88 | doAnswer(invocation -> {
|
68 | 89 | nameWrapper.set(invocation.getArgument(0));
|
69 | 90 | return null;
|
70 | 91 | }).when(nonSimple)
|
71 |
| - .setName(ArgumentMatchers.anyString()); |
72 |
| - nonSimple.setName("John"); |
73 |
| - Assertions.assertEquals(testee.getName(nonSimple), "John"); |
74 |
| - nonSimple.setName("Nick"); |
75 |
| - Assertions.assertEquals(testee.getName(nonSimple), "Nick"); |
| 92 | + .setName(anyString()); |
| 93 | + ExampleService srv = new ExampleService(); |
| 94 | + srv.setField(nonSimple::setName, "John"); |
| 95 | + assertEquals(srv.getField(nonSimple::getName), "John"); |
| 96 | + srv.setField(nonSimple::setName, "Nick"); |
| 97 | + assertEquals(srv.getField(nonSimple::getName), "Nick"); |
76 | 98 | }
|
77 | 99 |
|
78 | 100 | }
|
0 commit comments