1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.fail;
4
5 import java.util.Date;
6
7 import mockit.internal.expectations.invocation.MissingInvocation;
8 import mockit.internal.expectations.invocation.UnexpectedInvocation;
9
10 import org.junit.Test;
11
12
13
14
15 public final class ExpectationsWithInvocationCountsTest {
16
17
18 private final CodeUnderTest codeUnderTest = new CodeUnderTest();
19
20
21
22
23 static class CodeUnderTest {
24
25
26 private final Collaborator dependency = new Collaborator();
27
28
29
30
31 void doSomething() {
32 dependency.provideSomeService();
33 }
34
35
36
37
38 void doSomethingElse() {
39 dependency.simpleOperation(1, "b", null);
40 }
41 }
42
43
44
45
46 static class Collaborator {
47
48
49
50
51 Collaborator() {
52 }
53
54
55
56
57
58
59
60 Collaborator(@SuppressWarnings("unused") int value) {
61 }
62
63
64
65
66 void provideSomeService() {
67 }
68
69
70
71
72
73
74
75
76
77
78
79 @SuppressWarnings("UnusedDeclaration")
80 final void simpleOperation(int a, String b, Date c) {
81 }
82 }
83
84
85
86
87
88
89
90 @Test
91 public void expectOnce(@Mocked final Collaborator mock) {
92 new Expectations() {
93 {
94 mock.provideSomeService();
95 }
96 };
97
98 codeUnderTest.doSomething();
99 }
100
101
102
103
104
105
106
107 @Test(expected = UnexpectedInvocation.class)
108 public void expectOnceButReplayTwice(@Mocked final Collaborator mock) {
109 new Expectations() {
110 {
111 mock.provideSomeService();
112 times = 1;
113 }
114 };
115
116 codeUnderTest.doSomething();
117 codeUnderTest.doSomething();
118
119 fail("Should not get here");
120 }
121
122
123
124
125
126
127
128 @Test(expected = UnexpectedInvocation.class)
129 public void expectOnceButReplayMoreTimes(@Mocked final Collaborator mock) {
130 new Expectations() {
131 {
132 mock.provideSomeService();
133 times = 1;
134 }
135 };
136
137 codeUnderTest.doSomething();
138
139 try {
140 codeUnderTest.doSomething();
141 } finally {
142 codeUnderTest.doSomethingElse();
143 }
144
145 fail("Should not get here");
146 }
147
148
149
150
151
152
153
154 @Test
155 public void catchUnexpectedInvocationAndContinue(@Mocked final Collaborator mock) {
156 new Expectations() {
157 {
158 mock.provideSomeService();
159 maxTimes = 0;
160 }
161 };
162
163 try {
164 mock.provideSomeService();
165 } catch (UnexpectedInvocation ignore) {
166 }
167
168 mock.simpleOperation(1, "", null);
169 }
170
171
172
173
174
175
176
177 @Test
178 public void expectTwiceByUsingInvocationCount(@Mocked final Collaborator mock) {
179 new Expectations() {
180 {
181 mock.provideSomeService();
182 times = 2;
183 mock.simpleOperation(1, "b", null);
184 }
185 };
186
187 codeUnderTest.doSomething();
188 codeUnderTest.doSomething();
189 codeUnderTest.doSomethingElse();
190 }
191
192
193
194
195
196
197
198 @Test(expected = MissingInvocation.class)
199 public void expectTwiceByUsingInvocationCountButReplayOnlyOnce(@Mocked final Collaborator mock) {
200 new Expectations() {
201 {
202 mock.provideSomeService();
203 times = 2;
204 mock.simpleOperation(1, "b", null);
205 }
206 };
207
208 codeUnderTest.doSomething();
209 codeUnderTest.doSomethingElse();
210 }
211
212
213
214
215
216
217
218 @Test(expected = UnexpectedInvocation.class)
219 public void expectExactlyTwiceButReplayMoreTimes(@Mocked final Collaborator mock) {
220 new Expectations() {
221 {
222 mock.provideSomeService();
223 times = 2;
224 }
225 };
226
227 codeUnderTest.doSomething();
228 codeUnderTest.doSomething();
229 codeUnderTest.doSomething();
230 }
231
232
233
234
235
236
237
238 @Test
239 public void expectAtLeastOnceAndReplayTwice(@Mocked final Collaborator mock) {
240 new Expectations() {
241 {
242 mock.provideSomeService();
243 minTimes = 1;
244 mock.simpleOperation(1, "b", null);
245 }
246 };
247
248 codeUnderTest.doSomething();
249 codeUnderTest.doSomething();
250 codeUnderTest.doSomethingElse();
251 }
252
253
254
255
256
257
258
259 @Test(expected = MissingInvocation.class)
260 public void expectAtLeastTwiceButReplayOnceWithSingleExpectation(@Mocked final Collaborator mock) {
261 new Expectations() {
262 {
263 mock.provideSomeService();
264 minTimes = 2;
265 }
266 };
267
268 codeUnderTest.doSomething();
269 }
270
271
272
273
274
275
276
277 @Test(expected = MissingInvocation.class)
278 public void expectAtLeastTwiceButReplayOnceWithTwoConsecutiveExpectations(@Mocked final Collaborator mock) {
279 new Expectations() {
280 {
281 mock.provideSomeService();
282 minTimes = 2;
283 mock.simpleOperation(1, "b", null);
284 }
285 };
286
287 codeUnderTest.doSomething();
288 codeUnderTest.doSomethingElse();
289 }
290
291
292
293
294
295
296
297 @Test
298 public void repeatsAtLeastOverwritingUpperLimit(@Mocked final Collaborator mock) {
299 new Expectations() {
300 {
301 mock.provideSomeService();
302 maxTimes = 2;
303 minTimes = 1;
304 }
305 };
306
307 codeUnderTest.doSomething();
308 codeUnderTest.doSomething();
309 codeUnderTest.doSomething();
310 }
311
312
313
314
315
316
317
318 @Test
319 public void expectAtMostTwiceAndReplayOnce(@Mocked final Collaborator mock) {
320 new Expectations() {
321 {
322 mock.provideSomeService();
323 maxTimes = 2;
324 mock.simpleOperation(1, "b", null);
325 }
326 };
327
328 codeUnderTest.doSomething();
329 codeUnderTest.doSomethingElse();
330 }
331
332
333
334
335
336
337
338 @Test(expected = UnexpectedInvocation.class)
339 public void expectAtMostOnceButReplayTwice(@Mocked final Collaborator mock) {
340 new Expectations() {
341 {
342 mock.provideSomeService();
343 maxTimes = 1;
344 mock.simpleOperation(1, "b", null);
345 }
346 };
347
348 codeUnderTest.doSomething();
349 codeUnderTest.doSomething();
350 codeUnderTest.doSomethingElse();
351 }
352
353
354
355
356
357
358
359 @Test(expected = MissingInvocation.class)
360 public void repeatsAtMostDoesNotOverwriteLowerLimit(@Mocked final Collaborator mock) {
361 new Expectations() {
362 {
363 mock.provideSomeService();
364 minTimes = 2;
365 maxTimes = 3;
366 }
367 };
368
369 codeUnderTest.doSomething();
370 }
371
372
373
374
375
376
377
378 @Test
379 public void expectSameMethodOnceOrTwiceThenOnceButReplayEachExpectationOnlyOnce(@Mocked final Collaborator mock) {
380 new Expectations() {
381 {
382 mock.simpleOperation(1, "", null);
383 minTimes = 1;
384 maxTimes = 2;
385 mock.simpleOperation(2, "", null);
386 }
387 };
388
389 mock.simpleOperation(1, "", null);
390 mock.simpleOperation(2, "", null);
391 }
392
393
394
395
396
397
398
399 @Test
400 public void expectTwoOrThreeTimes(@Mocked final Collaborator mock) {
401 new Expectations() {
402 {
403 mock.provideSomeService();
404 minTimes = 2;
405 maxTimes = 3;
406 mock.simpleOperation(1, "b", null);
407 }
408 };
409
410 codeUnderTest.doSomething();
411 codeUnderTest.doSomething();
412 codeUnderTest.doSomethingElse();
413 }
414
415
416
417
418
419
420
421 @Test
422 public void expectZeroOrMoreTimesAndReplayTwice(@Mocked final Collaborator mock) {
423 new Expectations() {
424 {
425 mock.provideSomeService();
426 minTimes = 0;
427 maxTimes = -1;
428 mock.simpleOperation(1, "b", null);
429 }
430 };
431
432 codeUnderTest.doSomething();
433 codeUnderTest.doSomething();
434 codeUnderTest.doSomethingElse();
435 }
436
437
438
439
440
441
442
443 @Test(expected = MissingInvocation.class)
444 public void expectAtLeastOneInvocationMatchingStrictExpectationButInvokeNone(@Mocked final Collaborator a) {
445 new Expectations() {
446 {
447 a.provideSomeService();
448 maxTimes = -1;
449 }
450 };
451
452
453 }
454
455
456
457
458
459
460
461 @Test(expected = MissingInvocation.class)
462 public void expectOneOrMoreInvocationsFollowedByAnotherWhichWontOccur_maxTimes(@Mocked final Collaborator mock) {
463 new Expectations() {
464 {
465 mock.provideSomeService();
466 maxTimes = -1;
467 mock.simpleOperation(1, null, null);
468 }
469 };
470
471 codeUnderTest.doSomething();
472 }
473
474
475
476
477
478
479
480 @Test(expected = MissingInvocation.class)
481 public void expectOneOrMoreInvocationsFollowedByAnotherWhichWontOccur_minTimes(@Mocked final Collaborator mock) {
482 new Expectations() {
483 {
484 mock.simpleOperation(1, anyString, null);
485 minTimes = 1;
486 mock.provideSomeService();
487 }
488 };
489
490 codeUnderTest.doSomethingElse();
491 }
492 }