1
2
3
4
5
6 package mockit;
7
8 import java.util.concurrent.AbstractExecutorService;
9
10 import javax.sql.DataSource;
11
12 import mockit.integration.junit5.ExpectedException;
13 import mockit.integration.junit5.JMockitExtension;
14 import mockit.internal.expectations.invocation.MissingInvocation;
15 import mockit.internal.expectations.invocation.UnexpectedInvocation;
16
17 import org.junit.jupiter.api.Assertions;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20 import org.w3c.dom.Attr;
21
22
23
24
25 @ExtendWith(JMockitExtension.class)
26 class MockInstanceMatchingTest {
27
28
29
30
31 static class Collaborator {
32
33
34 private int value;
35
36
37
38
39
40
41 int getValue() {
42 return value;
43 }
44
45
46
47
48
49
50
51 void setValue(int value) {
52 this.value = value;
53 }
54 }
55
56
57 @Mocked
58 Collaborator mock;
59
60
61
62
63
64
65
66 @Test
67 void matchOnMockInstance(@Mocked Collaborator otherInstance) {
68 new Expectations() {
69 {
70 mock.getValue();
71 result = 12;
72 }
73 };
74
75 int result = mock.getValue();
76 Assertions.assertEquals(12, result);
77
78 Collaborator another = new Collaborator();
79 Assertions.assertEquals(0, another.getValue());
80 }
81
82
83
84
85
86
87
88 @Test
89 @ExpectedException(MissingInvocation.class)
90 void recordOnMockInstanceButReplayOnDifferentInstance(@Mocked final Collaborator verifiedMock) {
91 new Expectations() {
92 {
93 verifiedMock.getValue();
94 result = 12;
95 }
96 };
97
98 Collaborator collaborator = new Collaborator();
99 Assertions.assertEquals(0, collaborator.getValue());
100 }
101
102
103
104
105
106
107
108 @Test
109 void verifyExpectationMatchingOnMockInstance(@Mocked final Collaborator verifiedMock) {
110 new Collaborator().setValue(12);
111 verifiedMock.setValue(12);
112
113 new Verifications() {
114 {
115 verifiedMock.setValue(12);
116 times = 1;
117 }
118 };
119 }
120
121
122
123
124
125
126
127 @Test
128 void verifyExpectationsOnSameMethodCallForDifferentMockedInstances(@Mocked final Collaborator verifiedMock) {
129 final Collaborator c1 = new Collaborator();
130 c1.getValue();
131 verifiedMock.getValue();
132 final Collaborator c2 = new Collaborator();
133 c2.getValue();
134
135 new Verifications() {
136 {
137 verifiedMock.getValue();
138 times = 1;
139 c1.getValue();
140 times = 1;
141 c2.getValue();
142 times = 1;
143 }
144 };
145 }
146
147
148
149
150
151
152
153 @Test
154 @ExpectedException(MissingInvocation.class)
155 void verifyOnMockInstanceButReplayOnDifferentInstance(@Mocked final Collaborator verifiedMock) {
156 new Collaborator().setValue(12);
157
158 new Verifications() {
159 {
160 verifiedMock.setValue(12);
161 }
162 };
163 }
164
165
166
167
168
169
170
171 @Test
172 void recordExpectationsMatchingOnMultipleMockInstances(@Mocked final Collaborator mock2) {
173 new Expectations() {
174 {
175 mock.getValue();
176 result = 12;
177 mock2.getValue();
178 result = 13;
179 mock.setValue(20);
180 }
181 };
182
183 Assertions.assertEquals(12, mock.getValue());
184 Assertions.assertEquals(13, mock2.getValue());
185 mock.setValue(20);
186 }
187
188
189
190
191
192
193
194 @Test
195 @ExpectedException(MissingInvocation.class)
196 void recordOnSpecificMockInstancesButReplayOnDifferentOnes(@Mocked final Collaborator mock2) {
197 new Expectations() {
198 {
199 mock.setValue(12);
200 mock2.setValue(13);
201 }
202 };
203
204 mock2.setValue(12);
205 mock.setValue(13);
206 }
207
208
209
210
211
212
213
214 @Test
215 void verifyExpectationsMatchingOnMultipleMockInstances(@Mocked final Collaborator mock2) {
216 mock.setValue(12);
217 mock2.setValue(13);
218 mock.setValue(20);
219
220 new VerificationsInOrder() {
221 {
222 mock.setValue(12);
223 mock2.setValue(13);
224 mock.setValue(20);
225 }
226 };
227 }
228
229
230
231
232
233
234
235 @Test
236 @ExpectedException(UnexpectedInvocation.class)
237 void verifyOnSpecificMockInstancesButReplayOnDifferentOnes(@Mocked final Collaborator mock2) {
238 mock2.setValue(12);
239 mock.setValue(13);
240
241 Assertions.assertThrows(MissingInvocation.class, () -> {
242 new FullVerifications() {
243 {
244 mock.setValue(12);
245 mock2.setValue(13);
246 }
247 };
248 });
249 }
250
251
252
253
254
255
256
257 @Test
258 void matchOnTwoMockInstances(@Mocked final Collaborator mock2) {
259 new Expectations() {
260 {
261 mock.getValue();
262 result = 1;
263 times = 1;
264 mock2.getValue();
265 result = 2;
266 times = 1;
267 }
268 };
269
270 Assertions.assertEquals(1, mock.getValue());
271 Assertions.assertEquals(2, mock2.getValue());
272 }
273
274
275
276
277
278
279
280 @Test
281 void matchOnTwoMockInstancesAndReplayInDifferentOrder(@Mocked final Collaborator mock2) {
282 new Expectations() {
283 {
284 mock.getValue();
285 result = 1;
286 mock2.getValue();
287 result = 2;
288 }
289 };
290
291 Assertions.assertEquals(2, mock2.getValue());
292 Assertions.assertEquals(1, mock.getValue());
293 Assertions.assertEquals(1, mock.getValue());
294 Assertions.assertEquals(2, mock2.getValue());
295 }
296
297
298
299
300
301
302
303 @Test
304 void matchOnTwoMockInstancesForOtherwiseIdenticalExpectations(@Mocked final Collaborator mock2) {
305 mock.getValue();
306 mock2.getValue();
307 mock2.setValue(1);
308 mock.setValue(1);
309
310 new Verifications() {
311 {
312 mock.getValue();
313 times = 1;
314 mock2.getValue();
315 times = 1;
316 }
317 };
318
319 new VerificationsInOrder() {
320 {
321 mock2.setValue(1);
322 mock.setValue(1);
323 }
324 };
325 }
326
327
328
329
330
331
332
333
334
335 @Test
336 @ExpectedException(MissingInvocation.class)
337 void verifyExpectationsMatchingOnMultipleMockParametersButReplayedOutOfOrder(
338 @Mocked final AbstractExecutorService es1, @Mocked final AbstractExecutorService es2) {
339 es2.execute(null);
340 es1.submit((Runnable) null);
341
342 new VerificationsInOrder() {
343 {
344 es1.execute((Runnable) any);
345 es2.submit((Runnable) any);
346 }
347 };
348 }
349
350
351
352
353 @Test
354 void recordExpectationMatchingOnInstanceCreatedInsideCodeUnderTest() {
355 new Expectations() {
356 {
357 new Collaborator().getValue();
358 result = 1;
359 }
360 };
361
362 Assertions.assertEquals(1, new Collaborator().getValue());
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 @Test
379 void recordExpectationsOnTwoInstancesOfSameMockedInterface(@Mocked final DataSource mockDS1,
380 @Mocked final DataSource mockDS2, @Mocked Attr n) throws Exception {
381 new Expectations() {
382 {
383 mockDS1.getLoginTimeout();
384 result = 1000;
385 mockDS2.getLoginTimeout();
386 result = 2000;
387 }
388 };
389
390 Assertions.assertNotSame(mockDS1, mockDS2);
391 Assertions.assertEquals(1000, mockDS1.getLoginTimeout());
392 Assertions.assertEquals(2000, mockDS2.getLoginTimeout());
393 mockDS2.setLoginTimeout(3000);
394
395 new Verifications() {
396 {
397 mockDS2.setLoginTimeout(anyInt);
398 }
399 };
400 }
401
402
403
404
405 static class BaseClass {
406
407
408
409 final void doSomething() {
410 }
411 }
412
413
414
415
416 static final class SubclassA extends BaseClass {
417
418
419
420 void doSomethingElse() {
421 }
422 }
423
424
425
426
427 static final class SubclassB extends BaseClass {
428
429
430
431 void doSomethingElse() {
432 }
433 }
434
435
436
437
438
439
440
441
442
443
444
445 @Test
446 void verifyingCallsOnSpecificInstancesOfDifferentSubclasses(@Mocked SubclassA anyA, @Mocked final SubclassA a,
447 @Mocked final SubclassB anyB) {
448 a.doSomething();
449 new BaseClass().doSomething();
450 anyB.doSomething();
451 a.doSomethingElse();
452 new SubclassA().doSomethingElse();
453 anyB.doSomethingElse();
454
455 new Verifications() {
456 {
457 a.doSomethingElse();
458 times = 1;
459 anyB.doSomethingElse();
460 times = 1;
461 a.doSomething();
462 times = 1;
463 anyB.doSomething();
464 times = 1;
465 }
466 };
467 }
468 }