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