1 package mockit;
2
3 import mockit.internal.expectations.invocation.MissingInvocation;
4 import mockit.internal.expectations.invocation.UnexpectedInvocation;
5
6 import org.junit.Rule;
7 import org.junit.Test;
8 import org.junit.jupiter.api.Assertions;
9 import org.junit.rules.ExpectedException;
10
11
12
13
14 public final class AssertionErrorMessagesTest {
15
16
17 @Rule
18 public final ExpectedException thrown = ExpectedException.none();
19
20
21
22
23 static class Collaborator {
24
25
26
27
28 void doSomething() {
29 }
30
31
32
33
34
35
36
37
38
39 void doSomething(@SuppressWarnings("unused") int i, @SuppressWarnings("unused") String s) {
40 }
41
42
43
44
45
46
47
48 void doSomethingElse(@SuppressWarnings("unused") String s) {
49 }
50 }
51
52
53 @Mocked
54 Collaborator mock;
55
56
57
58
59 @Test
60 public void unexpectedInvocationForRecordedExpectation() {
61 new Expectations() {
62 {
63 mock.doSomething(anyInt, anyString);
64 times = 1;
65 }
66 };
67
68 mock.doSomething(1, "Abc");
69 Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> mock.doSomething(2, "xyz"));
70 Assertions.assertTrue(exception.getMessage().contains("Unexpected invocation to"));
71 Assertions.assertTrue(exception.getMessage().contains("doSomething(2, \"xyz\""));
72 }
73
74
75
76
77 @Test
78 public void unexpectedInvocationWhereExpectingAnotherForRecordedExpectations() {
79 mock.doSomething(1, "Abc");
80 mock.doSomething(2, "xyz");
81 mock.doSomethingElse("test");
82
83 Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> {
84 new VerificationsInOrder() {
85 {
86 mock.doSomething(anyInt, anyString);
87 times = 1;
88 mock.doSomethingElse(anyString);
89 }
90 };
91 });
92 Assertions.assertTrue(exception.getMessage().contains("doSomething(2, \"xyz\""));
93 }
94
95
96
97
98 @Test
99 public void unexpectedInvocationForRecordedExpectationWithMaximumInvocationCountOfZero() {
100 new Expectations() {
101 {
102 mock.doSomething(anyInt, anyString);
103 times = 0;
104 }
105 };
106
107 Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> mock.doSomething(1, "Abc"));
108 Assertions.assertTrue(exception.getMessage().contains("1, \"Abc\""));
109 }
110
111
112
113
114 @Test
115 public void unexpectedInvocationForVerifiedExpectation() {
116 mock.doSomething(123, "Test");
117 mock.doSomethingElse("abc");
118
119 Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> {
120 new Verifications() {
121 {
122 mock.doSomething(123, anyString);
123 times = 0;
124 }
125 };
126 });
127 Assertions.assertTrue(exception.getMessage().contains("123, \"Test\""));
128 }
129
130
131
132
133 @Test
134 public void unexpectedInvocationForExpectationsVerifiedInOrder() {
135 mock.doSomethingElse("test");
136 mock.doSomething(123, "Test");
137
138 Throwable exception = Assertions.assertThrows(UnexpectedInvocation.class, () -> {
139 new VerificationsInOrder() {
140 {
141 mock.doSomethingElse(anyString);
142 mock.doSomething(anyInt, anyString);
143 times = 0;
144 }
145 };
146 });
147 Assertions.assertTrue(exception.getMessage().contains("123, \"Test\""));
148 }
149
150
151
152
153 @Test
154 public void unexpectedInvocationOnMethodWithNoParameters() {
155 new Expectations() {
156 {
157 mock.doSomethingElse(anyString);
158 }
159 };
160
161 mock.doSomething();
162
163 thrown.expect(UnexpectedInvocation.class);
164 thrown.expectMessage("doSomething()\n on mock instance");
165 new FullVerifications(mock) {
166 };
167 }
168
169
170
171
172 @Test
173 public void missingInvocationForRecordedExpectation() {
174 new Expectations() {
175 {
176 mock.doSomething(anyInt, anyString);
177 times = 2;
178 }
179 };
180
181 thrown.expect(MissingInvocation.class);
182 thrown.expectMessage("any int, any String");
183
184 mock.doSomething(123, "Abc");
185 }
186
187
188
189
190 @Test
191 public void missingInvocationForRecordedExpectationWhichGetsNonMatchingInvocationsAtReplayTime() {
192 new Expectations() {
193 {
194 mock.doSomethingElse("test");
195 }
196 };
197
198 thrown.expect(MissingInvocation.class);
199 thrown.expectMessage("doSomethingElse(\"test\")");
200 thrown.expectMessage("instead got:");
201 thrown.expectMessage("doSomethingElse(\"Abc\")");
202 thrown.expectMessage("doSomethingElse(\"\")");
203
204 mock.doSomethingElse("Abc");
205 mock.doSomething(1, "xy");
206 mock.doSomethingElse("");
207 }
208
209
210
211
212 @Test
213 public void missingInvocationForVerifiedExpectation() {
214 Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
215 new Verifications() {
216 {
217 mock.doSomething(123, anyString);
218 }
219 };
220 });
221 Assertions.assertTrue(exception.getMessage().contains("123, any String"));
222 }
223
224
225
226
227 @Test
228 public void missingInvocationForVerifiedExpectationWhichGetsNonMatchingInvocationsAtReplayTime() {
229 mock.doSomethingElse("Abc");
230 mock.doSomething(1, "xy");
231 mock.doSomethingElse("");
232
233 Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
234 new Verifications() {
235 {
236 mock.doSomethingElse("test");
237 }
238 };
239 });
240 Assertions.assertTrue(exception.getMessage().contains("doSomethingElse(\"test\")"));
241 Assertions.assertTrue(exception.getMessage().contains("instead got:"));
242 Assertions.assertTrue(exception.getMessage().contains("doSomethingElse(\"Abc\")"));
243 Assertions.assertTrue(exception.getMessage().contains("doSomethingElse(\"\")"));
244 }
245
246
247
248
249 @Test
250 public void missingInvocationForExpectationVerifiedInOrder() {
251 mock.doSomething(123, "Test");
252
253 Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
254 new VerificationsInOrder() {
255 {
256 mock.doSomething(anyInt, anyString);
257 minTimes = 3;
258 }
259 };
260 });
261 Assertions.assertTrue(exception.getMessage().contains("any int, any String"));
262 }
263
264
265
266
267 @Test
268 public void missingInvocationForFullyVerifiedExpectations() {
269 mock.doSomething(123, "Abc");
270
271 Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
272 new FullVerifications() {
273 {
274 mock.doSomething(anyInt, anyString);
275 times = 2;
276 }
277 };
278 });
279 Assertions.assertTrue(exception.getMessage().contains("any int, any String"));
280 }
281
282
283
284
285 @Test
286 public void missingInvocationForExpectationUsingMatcherForDifferentParameterType() {
287 mock.doSomething(5, "");
288
289 Throwable exception = Assertions.assertThrows(MissingInvocation.class, () -> {
290 new Verifications() {
291 {
292 mock.doSomething(anyChar, "");
293 }
294 };
295 });
296 Assertions.assertTrue(exception.getMessage().contains("any char"));
297 }
298 }