• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.verification;
6 
7 import static org.assertj.core.api.Assertions.assertThatThrownBy;
8 import static org.junit.Assert.fail;
9 import static org.mockito.Mockito.atLeastOnce;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.verifyNoMoreInteractions;
13 import static org.mockito.Mockito.when;
14 
15 import java.util.List;
16 
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.exceptions.verification.NoInteractionsWanted;
20 import org.mockito.exceptions.verification.TooManyActualInvocations;
21 import org.mockito.exceptions.verification.WantedButNotInvoked;
22 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent;
23 import org.mockitousage.IMethods;
24 import org.mockitoutil.TestBase;
25 
26 public class BasicVerificationTest extends TestBase {
27 
28     @Mock private List<String> mock;
29     @Mock private List<String> mockTwo;
30 
31     @Test
shouldVerify()32     public void shouldVerify() {
33         mock.clear();
34         verify(mock).clear();
35 
36         mock.add("test");
37         verify(mock).add("test");
38 
39         verifyNoMoreInteractions(mock);
40     }
41 
42     @Test
shouldFailVerification()43     public void shouldFailVerification() {
44         assertThatThrownBy(
45                         () -> {
46                             verify(mock).clear();
47                         })
48                 .isInstanceOf(WantedButNotInvoked.class)
49                 .hasMessageContainingAll(
50                         "Wanted but not invoked:",
51                         "mock.clear();",
52                         "-> at ",
53                         "Actually, there were zero interactions with this mock.");
54     }
55 
56     @Test
shouldFailVerificationOnMethodArgument()57     public void shouldFailVerificationOnMethodArgument() {
58         mock.clear();
59         mock.add("foo");
60 
61         verify(mock).clear();
62 
63         assertThatThrownBy(
64                         () -> {
65                             verify(mock).add("bar");
66                         })
67                 .isInstanceOf(ArgumentsAreDifferent.class);
68     }
69 
70     @Test
shouldFailOnWrongMethod()71     public void shouldFailOnWrongMethod() {
72         mock.clear();
73         mock.clear();
74 
75         mockTwo.add("add");
76 
77         verify(mock, atLeastOnce()).clear();
78         verify(mockTwo, atLeastOnce()).add("add");
79         try {
80             verify(mockTwo, atLeastOnce()).add("foo");
81             fail();
82         } catch (WantedButNotInvoked e) {
83         }
84     }
85 
86     @Test
shouldDetectRedundantInvocation()87     public void shouldDetectRedundantInvocation() {
88         mock.clear();
89         mock.add("foo");
90         mock.add("bar");
91 
92         verify(mock).clear();
93         verify(mock).add("foo");
94 
95         try {
96             verifyNoMoreInteractions(mock);
97             fail();
98         } catch (NoInteractionsWanted e) {
99         }
100     }
101 
102     @Test
shouldDetectWhenInvokedMoreThanOnce()103     public void shouldDetectWhenInvokedMoreThanOnce() {
104         mock.add("foo");
105         mock.clear();
106         mock.clear();
107 
108         verify(mock).add("foo");
109 
110         try {
111             verify(mock).clear();
112             fail();
113         } catch (TooManyActualInvocations e) {
114         }
115     }
116 
117     @Test
shouldVerifyStubbedMethods()118     public void shouldVerifyStubbedMethods() {
119         when(mock.add("test")).thenReturn(Boolean.FALSE);
120 
121         mock.add("test");
122 
123         verify(mock).add("test");
124     }
125 
126     @Test
shouldDetectWhenOverloadedMethodCalled()127     public void shouldDetectWhenOverloadedMethodCalled() {
128         IMethods mockThree = mock(IMethods.class);
129 
130         mockThree.varargs((Object[]) new Object[] {});
131         try {
132             verify(mockThree).varargs((String[]) new String[] {});
133             fail();
134         } catch (WantedButNotInvoked e) {
135         }
136     }
137 }
138