|
| 1 | +package org.baeldung.mockito; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.mockito.ArgumentCaptor; |
| 12 | +import org.mockito.Captor; |
| 13 | +import org.mockito.InjectMocks; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.mockito.Mockito; |
| 16 | +import org.mockito.MockitoAnnotations; |
| 17 | +import org.mockito.Spy; |
| 18 | + |
| 19 | +//@RunWith(MockitoJUnitRunner.class) |
| 20 | +public class MockitoAnnotationTest { |
| 21 | + |
| 22 | + @Before |
| 23 | + public void init() { |
| 24 | + MockitoAnnotations.initMocks(this); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void whenNotUseMockAnnotation_thenCorrect() { |
| 29 | + final List mockList = Mockito.mock(List.class); |
| 30 | + mockList.add("one"); |
| 31 | + Mockito.verify(mockList).add("one"); |
| 32 | + assertEquals(0, mockList.size()); |
| 33 | + |
| 34 | + Mockito.when(mockList.size()).thenReturn(100); |
| 35 | + assertEquals(100, mockList.size()); |
| 36 | + } |
| 37 | + |
| 38 | + @Mock |
| 39 | + List<String> mockedList; |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenUseMockAnnotation_thenTheSame() { |
| 43 | + mockedList.add("one"); |
| 44 | + Mockito.verify(mockedList).add("one"); |
| 45 | + assertEquals(0, mockedList.size()); |
| 46 | + |
| 47 | + Mockito.when(mockedList.size()).thenReturn(100); |
| 48 | + assertEquals(100, mockedList.size()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenNotUseSpyAnnotation_thenCorrect() { |
| 53 | + final List<String> spyList = Mockito.spy(new ArrayList<String>()); |
| 54 | + spyList.add("one"); |
| 55 | + spyList.add("two"); |
| 56 | + |
| 57 | + Mockito.verify(spyList).add("one"); |
| 58 | + Mockito.verify(spyList).add("two"); |
| 59 | + |
| 60 | + assertEquals(2, spyList.size()); |
| 61 | + |
| 62 | + Mockito.doReturn(100).when(spyList).size(); |
| 63 | + assertEquals(100, spyList.size()); |
| 64 | + } |
| 65 | + |
| 66 | + @Spy |
| 67 | + List<String> spiedList = new ArrayList<String>(); |
| 68 | + |
| 69 | + @Test |
| 70 | + public void whenUseSpyAnnotation_thenTheSame() { |
| 71 | + spiedList.add("one"); |
| 72 | + spiedList.add("two"); |
| 73 | + |
| 74 | + Mockito.verify(spiedList).add("one"); |
| 75 | + Mockito.verify(spiedList).add("two"); |
| 76 | + |
| 77 | + assertEquals(2, spiedList.size()); |
| 78 | + |
| 79 | + Mockito.doReturn(100).when(spiedList).size(); |
| 80 | + assertEquals(100, spiedList.size()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void whenNotUseCaptorAnnotation_thenCorrect() { |
| 85 | + final List mockList = Mockito.mock(List.class); |
| 86 | + final ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class); |
| 87 | + mockList.add("one"); |
| 88 | + Mockito.verify(mockList).add(arg.capture()); |
| 89 | + |
| 90 | + assertEquals("one", arg.getValue()); |
| 91 | + } |
| 92 | + |
| 93 | + @Captor |
| 94 | + ArgumentCaptor<String> argCaptor; |
| 95 | + |
| 96 | + @Test |
| 97 | + public void whenUseCaptorAnnotation_thenTheSam() { |
| 98 | + mockedList.add("one"); |
| 99 | + Mockito.verify(mockedList).add(argCaptor.capture()); |
| 100 | + |
| 101 | + assertEquals("one", argCaptor.getValue()); |
| 102 | + } |
| 103 | + |
| 104 | + @Mock |
| 105 | + Map<String, String> wordMap; |
| 106 | + |
| 107 | + @InjectMocks |
| 108 | + MyDictionary dic = new MyDictionary(); |
| 109 | + |
| 110 | + @Test |
| 111 | + public void whenUseInjectMocksAnnotation_thenCorrect() { |
| 112 | + Mockito.when(wordMap.get("aWord")).thenReturn("aMeaning"); |
| 113 | + |
| 114 | + assertEquals("aMeaning", dic.getMeaning("aWord")); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments