1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertThrows;
4 import static org.junit.jupiter.api.Assertions.assertTrue;
5 import static org.junit.jupiter.api.Assertions.fail;
6
7 import java.util.concurrent.Callable;
8
9 import mockit.internal.expectations.invocation.MissingInvocation;
10 import mockit.internal.expectations.invocation.UnexpectedInvocation;
11
12 import org.junit.jupiter.api.Test;
13
14
15
16
17 final class RestrictedFullVerificationsTest {
18
19
20
21
22 @SuppressWarnings("UnusedReturnValue")
23 static class Dependency {
24
25
26
27
28
29
30
31 public void setSomething(@SuppressWarnings("unused") int value) {
32 }
33
34
35
36
37
38
39 public int editABunchMoreStuff() {
40 return 0;
41 }
42
43
44
45
46
47
48 public boolean prepare() {
49 return false;
50 }
51
52
53
54
55 public void save() {
56 }
57
58
59
60
61
62
63
64 static void staticMethod(@SuppressWarnings("unused") String s) {
65 }
66 }
67
68
69
70
71 static final class SubDependency extends Dependency {
72
73
74
75
76
77 int getValue() {
78 return 5;
79 }
80 }
81
82
83
84
85 static final class AnotherDependency {
86
87
88
89
90 void doSomething() {
91 }
92
93
94
95
96
97
98
99
100
101 String doSomethingElse(int i) {
102 return String.valueOf(i);
103 }
104
105
106
107
108
109
110 static boolean staticMethod() {
111 return true;
112 }
113 }
114
115
116 @Mocked
117 Dependency mock;
118
119
120
121
122 void exerciseCodeUnderTest() {
123 mock.prepare();
124 mock.setSomething(123);
125 mock.editABunchMoreStuff();
126 mock.save();
127 }
128
129
130
131
132
133
134
135 @Test
136 void verifyAllInvocationsToOnlyOneOfTwoMockedTypes(@Mocked AnotherDependency mock2) {
137 exerciseCodeUnderTest();
138 mock2.doSomething();
139
140 new FullVerifications(mock) {
141 {
142 mock.prepare();
143 mock.setSomething(anyInt);
144 minTimes = 1;
145 maxTimes = 2;
146 mock.editABunchMoreStuff();
147 mock.save();
148 times = 1;
149 }
150 };
151
152 new FullVerifications(mock.getClass()) {
153 {
154 mock.prepare();
155 mock.setSomething(anyInt);
156 minTimes = 1;
157 maxTimes = 2;
158 mock.editABunchMoreStuff();
159 mock.save();
160 times = 1;
161 }
162 };
163 }
164
165
166
167
168
169
170
171 @Test
172 void verifyAllInvocationsWithSomeMissing(@Mocked final AnotherDependency mock2) {
173 assertThrows(UnexpectedInvocation.class, () -> {
174 exerciseCodeUnderTest();
175 mock2.doSomething();
176
177 new FullVerifications(mock, mock2) {
178 {
179 mock.prepare();
180 mock.setSomething(anyInt);
181 mock.save();
182 mock2.doSomething();
183 }
184 };
185 });
186 }
187
188
189
190
191
192
193
194
195
196
197 @Test
198 void verifyOnlyInvocationsToGenericType(@Mocked final Callable<Dependency> mock2) throws Exception {
199 exerciseCodeUnderTest();
200 mock2.call();
201
202 new FullVerifications(mock2) {
203 {
204 mock2.call();
205 }
206 };
207 }
208
209
210
211
212
213
214
215 @Test
216 void verifyAllInvocationsToInheritedMethods(@Mocked SubDependency mock2) {
217 mock.prepare();
218 mock2.getValue();
219
220 new FullVerifications(mock) {
221 {
222 mock.prepare();
223 }
224 };
225 new FullVerifications(Dependency.class) {
226 {
227 mock.prepare();
228 }
229 };
230 }
231
232
233
234
235
236
237
238 @Test
239 void verifyAllInvocationsToInheritedMethods_whenNotVerified(@Mocked final SubDependency mock2) {
240 assertThrows(UnexpectedInvocation.class, () -> {
241 mock2.prepare();
242 mock2.getValue();
243
244 new FullVerifications(mock2) {
245 {
246 mock2.getValue();
247 }
248 };
249 });
250 }
251
252
253
254
255
256
257
258 @Test
259 void verifyAllInvocationsToSubclassMethods(@Mocked final SubDependency mock2) {
260 mock.prepare();
261 mock2.getValue();
262
263 new FullVerifications(mock2.getClass()) {
264 {
265 mock2.getValue();
266 }
267 };
268 }
269
270
271
272
273
274
275
276 @Test
277 void verifyAllInvocationsToSubclassMethods_whenNotVerified(@Mocked SubDependency mock2) {
278 assertThrows(UnexpectedInvocation.class, () -> {
279 mock.prepare();
280 mock2.getValue();
281
282 new FullVerifications(mock2.getClass()) {
283 {
284 mock.prepare();
285 }
286 };
287 });
288 }
289
290
291
292
293
294
295
296 @Test
297 void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass(@Mocked final SubDependency mock2) {
298 mock2.prepare();
299 mock2.getValue();
300
301 new FullVerifications(mock2) {
302 {
303 mock2.prepare();
304 mock2.getValue();
305 }
306 };
307 }
308
309
310
311
312
313
314
315 @Test
316 void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass_whenInheritedMethodNotVerified(
317 @Mocked final SubDependency mock2) {
318 assertThrows(UnexpectedInvocation.class, () -> {
319 mock2.prepare();
320 mock2.getValue();
321
322 new FullVerifications(mock2) {
323 {
324 mock2.getValue();
325 }
326 };
327 });
328 }
329
330
331
332
333
334
335
336 @Test
337 void verifyAllInvocationsToMethodsOfBaseClassAndOfSubclass_whenSubclassMethodNotVerified(
338 @Mocked SubDependency mock2) {
339 assertThrows(UnexpectedInvocation.class, () -> {
340 mock.prepare();
341 mock2.getValue();
342
343 new FullVerifications(mock2) {
344 {
345 mock.prepare();
346 }
347 };
348 });
349 }
350
351
352
353
354 @Test
355 void verifyAllInvocationsWithReplayOnDifferentInstance() {
356 new Dependency().save();
357
358 new FullVerifications(mock) {
359 {
360 new Dependency();
361 mock.save();
362 }
363 };
364 }
365
366
367
368
369
370
371
372 @Test
373 void verifyAllInvocationsWithReplayOnSameInstance(@Mocked final Dependency mock2) {
374 mock2.editABunchMoreStuff();
375
376 new FullVerifications(mock2) {
377 {
378 mock2.editABunchMoreStuff();
379 }
380 };
381 }
382
383
384
385
386
387
388
389 @Test
390 void verifyAllWithReplayOnDifferentInstanceWhenShouldBeSame(@Mocked Dependency mock2) {
391 assertThrows(MissingInvocation.class, () -> {
392 mock2.editABunchMoreStuff();
393
394 new FullVerifications(mock2) {
395 {
396 mock.editABunchMoreStuff();
397 }
398 };
399 });
400 }
401
402
403
404
405
406
407
408 @Test
409 void verifyAllWithUnverifiedReplayOnSameInstance(@Mocked Dependency mock2) {
410 assertThrows(UnexpectedInvocation.class, () -> {
411 mock.editABunchMoreStuff();
412 mock2.editABunchMoreStuff();
413
414 new FullVerifications(mock2) {
415 {
416 mock.editABunchMoreStuff();
417 }
418 };
419 });
420 }
421
422
423
424
425
426
427
428 @Test
429 void verifyStaticInvocationForSpecifiedMockInstance(@Mocked final AnotherDependency mock2) {
430 mock2.doSomething();
431 AnotherDependency.staticMethod();
432 mock2.doSomethingElse(1);
433 mock.editABunchMoreStuff();
434 mock2.doSomethingElse(2);
435
436 new FullVerifications(mock2) {
437 {
438 mock2.doSomething();
439 AnotherDependency.staticMethod();
440 mock2.doSomethingElse(anyInt);
441 mock2.doSomethingElse(anyInt);
442 }
443 };
444 }
445
446
447
448
449
450
451
452 @Test
453 void unverifiedStaticInvocationForSpecifiedMockInstance(@Mocked final AnotherDependency mock2) {
454 assertThrows(UnexpectedInvocation.class, () -> {
455 mock2.doSomething();
456 AnotherDependency.staticMethod();
457
458 new FullVerifications(mock2) {
459 {
460 mock2.doSomething();
461 }
462 };
463 });
464 }
465
466
467
468
469
470
471
472 @Test
473 void unverifiedStaticInvocationForSpecifiedSubclassInstance(@Mocked final SubDependency mock2) {
474 assertThrows(UnexpectedInvocation.class, () -> {
475 mock2.getValue();
476 Dependency.staticMethod("test");
477
478 new FullVerifications(mock2) {
479 {
480 mock2.getValue();
481 }
482 };
483 });
484 }
485
486
487
488
489
490
491
492 @Test
493 void verifyNoInvocationsOccurredOnOneOfTwoMockedDependencies(@Mocked AnotherDependency mock2) {
494 mock2.doSomething();
495
496 new FullVerifications(mock) {
497 };
498 }
499
500
501
502
503
504
505
506 @Test
507 void verifyNoInvocationsOccurredOnMockedDependencyWithOneHavingOccurred(@Mocked AnotherDependency mock2) {
508 mock2.doSomething();
509 mock.editABunchMoreStuff();
510
511 try {
512 new FullVerifications(mock) {
513 };
514 fail();
515 } catch (UnexpectedInvocation e) {
516 assertTrue(e.getMessage().contains("editABunchMoreStuff()"));
517 }
518 }
519
520
521
522
523
524
525
526 @Test
527 void verifyNoInvocationsOnOneOfTwoMockedDependenciesBeyondThoseRecordedAsExpected(
528 @Mocked final AnotherDependency mock2) {
529 new Expectations() {
530 {
531 mock.setSomething(anyInt);
532 mock2.doSomething();
533 times = 1;
534 }
535 };
536
537 mock.prepare();
538 mock.setSomething(1);
539 mock.setSomething(2);
540 mock.save();
541 mock2.doSomething();
542
543 new FullVerifications(mock2) {
544 };
545 }
546 }