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.rules.ExpectedException;
9
10
11
12
13 public final class VerificationsInOrderTest {
14
15
16 @Rule
17 public final ExpectedException thrown = ExpectedException.none();
18
19
20
21
22 @SuppressWarnings("unused")
23 public static class Dependency {
24
25
26
27
28
29
30
31 public void setSomething(int value) {
32 }
33
34
35
36
37
38
39
40 public void setSomethingElse(String value) {
41 }
42
43
44
45
46 public void editABunchMoreStuff() {
47 }
48
49
50
51
52 public void notifyBeforeSave() {
53 }
54
55
56
57
58 public void prepare() {
59 }
60
61
62
63
64 public void save() {
65 }
66
67
68
69
70
71
72
73 void doSomething(ClassWithHashCode h) {
74 }
75 }
76
77
78
79
80 static final class ClassWithHashCode {
81 @Override
82 public boolean equals(Object obj) {
83 return obj instanceof ClassWithHashCode && this == obj;
84 }
85
86 @Override
87 public int hashCode() {
88 return 123;
89 }
90 }
91
92
93 @Mocked
94 Dependency mock;
95
96
97
98
99 void exerciseCodeUnderTest() {
100 mock.prepare();
101 mock.setSomething(123);
102 mock.setSomethingElse("anotherValue");
103 mock.setSomething(45);
104 mock.editABunchMoreStuff();
105 mock.notifyBeforeSave();
106 mock.save();
107 }
108
109
110
111
112 @Test
113 public void verifySimpleInvocations() {
114 exerciseCodeUnderTest();
115
116 new VerificationsInOrder() {
117 {
118 mock.prepare();
119 mock.setSomething(45);
120 mock.editABunchMoreStuff();
121 }
122 };
123 }
124
125
126
127
128 @Test
129 public void verifyUnrecordedInvocationThatShouldHappenButDoesNot() {
130 thrown.expect(MissingInvocation.class);
131
132 mock.setSomething(1);
133
134 new VerificationsInOrder() {
135 {
136 mock.notifyBeforeSave();
137 }
138 };
139 }
140
141
142
143
144 @Test
145 public void verifyUnrecordedInvocationThatShouldHappenExactlyOnceButDoesNot() {
146 thrown.expect(MissingInvocation.class);
147 thrown.expectMessage("2");
148
149 mock.setSomething(1);
150
151 new VerificationsInOrder() {
152 {
153 mock.setSomething(2);
154 times = 1;
155 }
156 };
157 }
158
159
160
161
162 @Test
163 public void verifyRecordedInvocationThatShouldHappenButDoesNot() {
164 thrown.expect(MissingInvocation.class);
165
166 new Expectations() {
167 {
168 mock.setSomething(1);
169 mock.notifyBeforeSave();
170 }
171 };
172
173 mock.setSomething(1);
174
175 new VerificationsInOrder() {
176 {
177 mock.setSomething(1);
178 mock.notifyBeforeSave();
179 }
180 };
181 }
182
183
184
185
186 @Test
187 public void verifyAllInvocationsWithSomeOfThemRecorded() {
188 new Expectations() {
189 {
190 mock.prepare();
191 mock.editABunchMoreStuff();
192 }
193 };
194
195 exerciseCodeUnderTest();
196
197 new VerificationsInOrder() {
198 {
199 mock.prepare();
200 minTimes = 1;
201 mock.setSomethingElse(anyString);
202 mock.setSomething(anyInt);
203 minTimes = 1;
204 maxTimes = 2;
205 mock.editABunchMoreStuff();
206 mock.notifyBeforeSave();
207 maxTimes = 1;
208 mock.save();
209 times = 1;
210 }
211 };
212 }
213
214
215
216
217 @Test
218 public void verifyInvocationsWithExactInvocationCountsHavingRecordedMatchingExpectationWithArgumentMatcher() {
219 new Expectations() {
220 {
221 mock.setSomething(anyInt);
222 }
223 };
224
225 mock.setSomething(1);
226 mock.setSomething(2);
227
228 new VerificationsInOrder() {
229 {
230 mock.setSomething(1);
231 times = 1;
232 mock.setSomething(2);
233 times = 1;
234 }
235 };
236 }
237
238
239
240
241 @Test
242 public void verifyInvocationThatIsAllowedToHappenAnyNumberOfTimesAndHappensOnce() {
243 mock.prepare();
244 mock.setSomething(123);
245 mock.save();
246
247 new VerificationsInOrder() {
248 {
249 mock.prepare();
250 mock.setSomething(anyInt);
251 mock.save();
252 }
253 };
254 }
255
256
257
258
259 @Test
260 public void verifySimpleInvocationsWhenOutOfOrder() {
261 thrown.expect(MissingInvocation.class);
262 thrown.expectMessage("123");
263
264 mock.setSomething(123);
265 mock.prepare();
266
267 new VerificationsInOrder() {
268 {
269 mock.prepare();
270 mock.setSomething(123);
271 }
272 };
273 }
274
275
276
277
278 @Test
279 public void verifyRepeatingInvocation() {
280 mock.setSomething(123);
281 mock.setSomething(123);
282
283 new VerificationsInOrder() {
284 {
285 mock.setSomething(123);
286 times = 2;
287 }
288 };
289 }
290
291
292
293
294 @Test
295 public void verifyRepeatingInvocationThatOccursOneTimeMoreThanExpected() {
296 thrown.expect(UnexpectedInvocation.class);
297
298 mock.setSomething(123);
299 mock.setSomething(123);
300
301 new VerificationsInOrder() {
302 {
303 mock.setSomething(123);
304 maxTimes = 1;
305 }
306 };
307 }
308
309
310
311
312 @Test
313 public void verifyRepeatingInvocationUsingMatcher() {
314 mock.setSomething(123);
315 mock.setSomething(45);
316
317 new VerificationsInOrder() {
318 {
319 mock.setSomething(anyInt);
320 times = 2;
321 }
322 };
323 }
324
325
326
327
328 @Test
329 public void verifyInvocationNotExpectedToOccurButWhichDoes() {
330 thrown.expect(UnexpectedInvocation.class);
331 thrown.expectMessage("123");
332
333 mock.prepare();
334 mock.setSomething(123);
335
336 new VerificationsInOrder() {
337 {
338 mock.prepare();
339 mock.setSomething(anyInt);
340 maxTimes = 0;
341 }
342 };
343 }
344
345
346
347
348 @Test
349 public void verifyWithArgumentMatcher() {
350 exerciseCodeUnderTest();
351
352 new VerificationsInOrder() {
353 {
354 mock.prepare();
355 mock.setSomething(anyInt);
356 }
357 };
358 }
359
360
361
362
363 @Test
364 public void verifyWithIndividualInvocationCountsForNonConsecutiveInvocations() {
365 exerciseCodeUnderTest();
366
367 new VerificationsInOrder() {
368 {
369 mock.prepare();
370 times = 1;
371 mock.setSomething(anyInt);
372 times = 2;
373 }
374 };
375 }
376
377
378
379
380
381
382
383 @Test
384 public void verifyUsingInvocationCountConstraintAndArgumentMatcherOnObjectWithMockedHashCode(
385 @Mocked ClassWithHashCode wh) {
386 mock.doSomething(null);
387 mock.doSomething(wh);
388
389 new VerificationsInOrder() {
390 {
391 mock.doSomething((ClassWithHashCode) withNull());
392 times = 1;
393 mock.doSomething((ClassWithHashCode) withNotNull());
394 }
395 };
396 }
397
398
399
400
401 @Test
402 public void verifyWithArgumentMatchersWhenOutOfOrder() {
403 thrown.expect(MissingInvocation.class);
404 thrown.expectMessage("any String");
405
406 mock.setSomething(123);
407 mock.setSomethingElse("anotherValue");
408 mock.setSomething(45);
409
410 new VerificationsInOrder() {
411 {
412 mock.setSomething(anyInt);
413 mock.setSomething(anyInt);
414 mock.setSomethingElse(anyString);
415 }
416 };
417 }
418
419
420
421
422 @Test
423 public void verifyWithArgumentMatcherAndIndividualInvocationCountWhenOutOfOrder() {
424 thrown.expect(MissingInvocation.class);
425 thrown.expectMessage("Missing 1 invocation");
426 thrown.expectMessage("any int");
427
428 mock.setSomething(123);
429 mock.prepare();
430 mock.setSomething(45);
431
432 new VerificationsInOrder() {
433 {
434 mock.prepare();
435 mock.setSomething(anyInt);
436 times = 2;
437 }
438 };
439 }
440
441
442
443
444 @Test
445 public void verifyTwoIndependentSequencesOfInvocationsWhichOccurSeparately() {
446
447 mock.setSomething(1);
448 mock.setSomething(2);
449
450
451 mock.setSomething(10);
452 mock.setSomething(20);
453
454
455 new VerificationsInOrder() {
456 {
457 mock.setSomething(1);
458 mock.setSomething(2);
459 }
460 };
461
462
463 new VerificationsInOrder() {
464 {
465 mock.setSomething(10);
466 mock.setSomething(20);
467 }
468 };
469 }
470
471
472
473
474 @Test
475 public void verifyTwoIndependentSequencesOfInvocationsWhichAreMixedTogether() {
476 mock.setSomething(1);
477 mock.setSomething(10);
478 mock.setSomething(2);
479 mock.setSomething(20);
480
481
482 new VerificationsInOrder() {
483 {
484 mock.setSomething(10);
485 mock.setSomething(20);
486 }
487 };
488
489
490 new VerificationsInOrder() {
491 {
492 mock.setSomething(1);
493 mock.setSomething(2);
494 }
495 };
496 }
497
498
499
500
501 @Test
502 public void verifySecondSequenceOfInvocationsWithTimesConstraintAfterVerifyingLastInvocationOfFirstSequence() {
503 mock.setSomething(1);
504 mock.setSomething(3);
505 mock.setSomething(4);
506 mock.setSomething(2);
507
508 new VerificationsInOrder() {
509 {
510 mock.setSomething(1);
511 mock.setSomething(2);
512 }
513 };
514
515 new VerificationsInOrder() {
516 {
517 mock.setSomething(3);
518 mock.setSomething(4);
519 times = 1;
520 }
521 };
522 }
523 }