|
1 | 1 | package org.baeldung.mockito; |
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.hasItem; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | +import static org.mockito.Matchers.anyString; |
3 | 6 | import static org.mockito.Mockito.atLeast; |
4 | 7 | import static org.mockito.Mockito.atMost; |
5 | 8 | import static org.mockito.Mockito.mock; |
|
12 | 15 | import java.util.List; |
13 | 16 |
|
14 | 17 | import org.junit.Test; |
| 18 | +import org.mockito.ArgumentCaptor; |
15 | 19 | import org.mockito.InOrder; |
16 | 20 | import org.mockito.Mockito; |
17 | 21 | import org.mockito.exceptions.verification.NoInteractionsWanted; |
18 | 22 |
|
| 23 | +import com.google.common.collect.Lists; |
| 24 | + |
19 | 25 | public class MockitoVerifyExamplesTest { |
20 | 26 |
|
21 | | - // tests |
22 | | - |
23 | | - @Test |
24 | | - public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() { |
25 | | - final List<String> mockedList = mock(MyList.class); |
26 | | - mockedList.size(); |
27 | | - verify(mockedList).size(); |
28 | | - } |
29 | | - |
30 | | - @Test |
31 | | - public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() { |
32 | | - final List<String> mockedList = mock(MyList.class); |
33 | | - mockedList.size(); |
34 | | - verify(mockedList, times(1)).size(); |
35 | | - } |
36 | | - |
37 | | - @Test |
38 | | - public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() { |
39 | | - final List<String> mockedList = mock(MyList.class); |
40 | | - verifyZeroInteractions(mockedList); |
41 | | - } |
42 | | - |
43 | | - @Test |
44 | | - public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() { |
45 | | - final List<String> mockedList = mock(MyList.class); |
46 | | - verify(mockedList, times(0)).size(); |
47 | | - } |
48 | | - |
49 | | - @Test(expected = NoInteractionsWanted.class) |
50 | | - public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() { |
51 | | - final List<String> mockedList = mock(MyList.class); |
52 | | - mockedList.size(); |
53 | | - mockedList.clear(); |
54 | | - |
55 | | - verify(mockedList).size(); |
56 | | - verifyNoMoreInteractions(mockedList); |
57 | | - } |
58 | | - |
59 | | - @Test |
60 | | - public final void whenVerifyingOrderOfInteractions_thenCorrect() { |
61 | | - final List<String> mockedList = mock(MyList.class); |
62 | | - mockedList.size(); |
63 | | - mockedList.add("a parameter"); |
64 | | - mockedList.clear(); |
65 | | - |
66 | | - final InOrder inOrder = Mockito.inOrder(mockedList); |
67 | | - inOrder.verify(mockedList).size(); |
68 | | - inOrder.verify(mockedList).add("a parameter"); |
69 | | - inOrder.verify(mockedList).clear(); |
70 | | - } |
71 | | - |
72 | | - @Test |
73 | | - public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() { |
74 | | - final List<String> mockedList = mock(MyList.class); |
75 | | - mockedList.size(); |
76 | | - |
77 | | - verify(mockedList, never()).clear(); |
78 | | - } |
79 | | - |
80 | | - @Test |
81 | | - public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() { |
82 | | - final List<String> mockedList = mock(MyList.class); |
83 | | - mockedList.clear(); |
84 | | - mockedList.clear(); |
85 | | - mockedList.clear(); |
86 | | - |
87 | | - verify(mockedList, atLeast(1)).clear(); |
88 | | - verify(mockedList, atMost(10)).clear(); |
89 | | - } |
| 27 | + // tests |
| 28 | + |
| 29 | + @Test |
| 30 | + public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() { |
| 31 | + final List<String> mockedList = mock(MyList.class); |
| 32 | + mockedList.size(); |
| 33 | + verify(mockedList).size(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() { |
| 38 | + final List<String> mockedList = mock(MyList.class); |
| 39 | + mockedList.size(); |
| 40 | + verify(mockedList, times(1)).size(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() { |
| 45 | + final List<String> mockedList = mock(MyList.class); |
| 46 | + verifyZeroInteractions(mockedList); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() { |
| 51 | + final List<String> mockedList = mock(MyList.class); |
| 52 | + verify(mockedList, times(0)).size(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test(expected = NoInteractionsWanted.class) |
| 56 | + public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() { |
| 57 | + final List<String> mockedList = mock(MyList.class); |
| 58 | + mockedList.size(); |
| 59 | + mockedList.clear(); |
| 60 | + |
| 61 | + verify(mockedList).size(); |
| 62 | + verifyNoMoreInteractions(mockedList); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public final void whenVerifyingOrderOfInteractions_thenCorrect() { |
| 67 | + final List<String> mockedList = mock(MyList.class); |
| 68 | + mockedList.size(); |
| 69 | + mockedList.add("a parameter"); |
| 70 | + mockedList.clear(); |
| 71 | + |
| 72 | + final InOrder inOrder = Mockito.inOrder(mockedList); |
| 73 | + inOrder.verify(mockedList).size(); |
| 74 | + inOrder.verify(mockedList).add("a parameter"); |
| 75 | + inOrder.verify(mockedList).clear(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() { |
| 80 | + final List<String> mockedList = mock(MyList.class); |
| 81 | + mockedList.size(); |
| 82 | + |
| 83 | + verify(mockedList, never()).clear(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() { |
| 88 | + final List<String> mockedList = mock(MyList.class); |
| 89 | + mockedList.clear(); |
| 90 | + mockedList.clear(); |
| 91 | + mockedList.clear(); |
| 92 | + |
| 93 | + verify(mockedList, atLeast(1)).clear(); |
| 94 | + verify(mockedList, atMost(10)).clear(); |
| 95 | + } |
| 96 | + |
| 97 | + // with arguments |
| 98 | + |
| 99 | + @Test |
| 100 | + public final void whenVerifyingAnInteractionWithExactArgument_thenCorrect() { |
| 101 | + final List<String> mockedList = mock(MyList.class); |
| 102 | + mockedList.add("test"); |
| 103 | + |
| 104 | + verify(mockedList).add("test"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public final void whenVerifyingAnInteractionWithAnyArgument_thenCorrect() { |
| 109 | + final List<String> mockedList = mock(MyList.class); |
| 110 | + mockedList.add("test"); |
| 111 | + |
| 112 | + verify(mockedList).add(anyString()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public final void whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() { |
| 117 | + final List<String> mockedList = mock(MyList.class); |
| 118 | + mockedList.addAll(Lists.<String> newArrayList("someElement")); |
| 119 | + final ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(List.class); |
| 120 | + verify(mockedList).addAll(argumentCaptor.capture()); |
| 121 | + final List<String> capturedArgument = argumentCaptor.<List<String>> getValue(); |
| 122 | + assertThat(capturedArgument, hasItem("someElement")); |
| 123 | + } |
90 | 124 |
|
91 | 125 | } |
0 commit comments