1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9 import static org.junit.jupiter.api.Assertions.assertEquals;
10 import static org.junit.jupiter.api.Assertions.assertFalse;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13 import static org.junit.jupiter.api.Assertions.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Iterator;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.ListIterator;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.SortedMap;
25 import java.util.SortedSet;
26 import java.util.concurrent.Callable;
27
28 import mockit.integration.junit5.JMockitExtension;
29
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32
33
34
35
36 @ExtendWith(JMockitExtension.class)
37 class ExpectationsWithValuesToReturnTest {
38
39
40
41
42 static class Collaborator {
43
44
45
46
47
48
49 static String doInternal() {
50 return "123";
51 }
52
53
54
55
56
57
58 int getValue() {
59 return -1;
60 }
61
62
63
64
65
66
67 Integer getInteger() {
68 return -1;
69 }
70
71
72
73
74
75
76 byte getByteValue() {
77 return -1;
78 }
79
80
81
82
83
84
85 Byte getByteWrapper() {
86 return -1;
87 }
88
89
90
91
92
93
94 short getShortValue() {
95 return -1;
96 }
97
98
99
100
101
102
103 Short getShortWrapper() {
104 return -1;
105 }
106
107
108
109
110
111
112 long getLongValue() {
113 return -1;
114 }
115
116
117
118
119
120
121 Long getLongWrapper() {
122 return -1L;
123 }
124
125
126
127
128
129
130 float getFloatValue() {
131 return -1.0F;
132 }
133
134
135
136
137
138
139 Float getFloatWrapper() {
140 return -1.0F;
141 }
142
143
144
145
146
147
148 double getDoubleValue() {
149 return -1.0;
150 }
151
152
153
154
155
156
157 Double getDoubleWrapper() {
158 return -1.0;
159 }
160
161
162
163
164
165
166 char getCharValue() {
167 return '1';
168 }
169
170
171
172
173
174
175 Character getCharacter() {
176 return '1';
177 }
178
179
180
181
182
183
184 boolean getBooleanValue() {
185 return true;
186 }
187
188
189
190
191
192
193 Boolean getBooleanWrapper() {
194 return true;
195 }
196
197
198
199
200
201
202 String getString() {
203 return "";
204 }
205
206
207
208
209
210
211 Object getObject() {
212 return null;
213 }
214
215
216
217
218
219
220 Collection<?> getItems() {
221 return null;
222 }
223
224
225
226
227
228
229 List<?> getListItems() {
230 return null;
231 }
232
233
234
235
236
237
238 List<?> getAList() {
239 return null;
240 }
241
242
243
244
245
246
247 Set<?> getSetItems() {
248 return null;
249 }
250
251
252
253
254
255
256 SortedSet<?> getSortedSetItems() {
257 return null;
258 }
259
260
261
262
263
264
265 Map<?, ?> getMapItems() {
266 return null;
267 }
268
269
270
271
272
273
274 SortedMap<?, ?> getSortedMapItems() {
275 return null;
276 }
277
278
279
280
281
282
283 ListIterator<?> getListIterator() {
284 return null;
285 }
286
287
288
289
290
291
292 Iterator<?> getIterator() {
293 return null;
294 }
295
296
297
298
299
300
301 Iterable<?> getIterable() {
302 return null;
303 }
304
305
306
307
308
309
310 int[] getIntArray() {
311 return null;
312 }
313
314
315
316
317
318
319 int[][] getInt2Array() {
320 return null;
321 }
322
323
324
325
326
327
328 byte[] getByteArray() {
329 return null;
330 }
331
332
333
334
335
336
337 Byte[] getByteWrapperArray() {
338 return null;
339 }
340
341
342
343
344
345
346 short[] getShortArray() {
347 return null;
348 }
349
350
351
352
353
354
355 Short[] getShortWrapperArray() {
356 return null;
357 }
358
359
360
361
362
363
364 long[] getLongArray() {
365 return null;
366 }
367
368
369
370
371
372
373 long[][] getLong2Array() {
374 return null;
375 }
376
377
378
379
380
381
382 float[] getFloatArray() {
383 return null;
384 }
385
386
387
388
389
390
391 double[] getDoubleArray() {
392 return null;
393 }
394
395
396
397
398
399
400 char[] getCharArray() {
401 return null;
402 }
403
404
405
406
407
408
409 boolean[] getBooleanArray() {
410 return null;
411 }
412
413
414
415
416
417
418 String[] getStringArray() {
419 return null;
420 }
421
422
423
424
425
426
427 String[][] getString2Array() {
428 return null;
429 }
430 }
431
432
433
434
435
436
437
438 @Test
439 void returnsDefaultValuesForPrimitiveAndWrapperReturnTypes(@Mocked final Collaborator mock) {
440 new Expectations() {
441 {
442 mock.getValue();
443 mock.getInteger();
444 mock.getByteValue();
445 mock.getByteWrapper();
446 mock.getShortValue();
447 mock.getShortWrapper();
448 mock.getLongValue();
449 mock.getLongWrapper();
450 mock.getFloatValue();
451 mock.getFloatWrapper();
452 mock.getDoubleValue();
453 mock.getDoubleWrapper();
454 mock.getCharValue();
455 mock.getCharacter();
456 mock.getBooleanValue();
457 mock.getBooleanWrapper();
458 Collaborator.doInternal();
459 }
460 };
461
462 assertEquals(0, mock.getValue());
463 assertEquals(0, mock.getInteger().intValue());
464 assertEquals((byte) 0, mock.getByteValue());
465 assertEquals((byte) 0, mock.getByteWrapper().byteValue());
466 assertEquals((short) 0, mock.getShortValue());
467 assertEquals((short) 0, mock.getShortWrapper().shortValue());
468 assertEquals(0L, mock.getLongValue());
469 assertEquals(0L, mock.getLongWrapper().longValue());
470 assertEquals(0.0F, mock.getFloatValue(), 0.0);
471 assertEquals(0, mock.getFloatWrapper(), 0);
472 assertEquals(0.0, mock.getDoubleValue(), 0.0);
473 assertEquals(0, mock.getDoubleWrapper(), 0);
474 assertEquals('\0', mock.getCharValue());
475 assertEquals('\0', mock.getCharacter().charValue());
476 assertFalse(mock.getBooleanValue());
477 assertFalse(mock.getBooleanWrapper());
478 assertNull(Collaborator.doInternal());
479 }
480
481
482
483
484
485
486
487 @Test
488 void returnsDefaultValuesForCollectionValuedReturnTypes(@Mocked final Collaborator mock) {
489 new Expectations() {
490 {
491 mock.getItems();
492 mock.getListItems();
493 mock.getSetItems();
494 mock.getSortedSetItems();
495 mock.getMapItems();
496 mock.getSortedMapItems();
497 }
498 };
499
500 assertSame(Collections.emptyList(), mock.getItems());
501 assertSame(Collections.emptyList(), mock.getListItems());
502 assertSame(Collections.emptySet(), mock.getSetItems());
503 assertEquals(Collections.emptySet(), mock.getSortedSetItems());
504 assertSame(Collections.emptyMap(), mock.getMapItems());
505 assertEquals(Collections.emptyMap(), mock.getSortedMapItems());
506 }
507
508
509
510
511
512
513
514 @Test
515 void returnsDefaultValuesForArrayValuedReturnTypes(@Mocked final Collaborator mock) {
516 new Expectations() {
517 {
518 mock.getIntArray();
519 mock.getInt2Array();
520 mock.getByteArray();
521 mock.getShortArray();
522 mock.getShortWrapperArray();
523 mock.getLongArray();
524 mock.getLong2Array();
525 mock.getFloatArray();
526 mock.getDoubleArray();
527 mock.getCharArray();
528 mock.getBooleanArray();
529 mock.getStringArray();
530 mock.getString2Array();
531 }
532 };
533
534 assertArrayEquals(new int[0], mock.getIntArray());
535 assertArrayEquals(new int[0][0], mock.getInt2Array());
536 assertArrayEquals(new byte[0], mock.getByteArray());
537 assertArrayEquals(new short[0], mock.getShortArray());
538 assertArrayEquals(new Short[0], mock.getShortWrapperArray());
539 assertArrayEquals(new long[0], mock.getLongArray());
540 assertArrayEquals(new long[0][0], mock.getLong2Array());
541 assertArrayEquals(new float[0], mock.getFloatArray(), 0.0F);
542 assertArrayEquals(new double[0], mock.getDoubleArray(), 0.0);
543 assertArrayEquals(new char[0], mock.getCharArray());
544 assertEquals(0, mock.getBooleanArray().length);
545 assertArrayEquals(new String[0], mock.getStringArray());
546 assertArrayEquals(new String[0][0], mock.getString2Array());
547 }
548
549
550
551
552 @Test
553 void returnsMultipleValuesInSequenceUsingVarargs() {
554 final Collaborator collaborator = new Collaborator();
555
556 new Expectations(collaborator) {
557 {
558 collaborator.getBooleanValue();
559 returns(true, false);
560 collaborator.getShortValue();
561 returns((short) 1, (short) 2, (short) 3);
562 collaborator.getShortWrapper();
563 returns((short) 5, (short) 6, (short) -7, (short) -8);
564 collaborator.getCharArray();
565 returns(new char[0], new char[] { 'x' });
566 }
567 };
568
569 assertTrue(collaborator.getBooleanValue());
570 assertFalse(collaborator.getBooleanValue());
571
572 assertEquals(1, collaborator.getShortValue());
573 assertEquals(2, collaborator.getShortValue());
574 assertEquals(3, collaborator.getShortValue());
575
576 assertEquals(5, collaborator.getShortWrapper().shortValue());
577 assertEquals(6, collaborator.getShortWrapper().shortValue());
578 assertEquals(-7, collaborator.getShortWrapper().shortValue());
579 assertEquals(-8, collaborator.getShortWrapper().shortValue());
580
581 assertArrayEquals(new char[0], collaborator.getCharArray());
582 assertArrayEquals(new char[] { 'x' }, collaborator.getCharArray());
583 }
584
585
586
587
588
589
590
591 @Test
592 void returnsNonNullValueFollowedByNullUsingVarargs(@Mocked final Collaborator collaborator) {
593 new Expectations() {
594 {
595 collaborator.getString();
596
597 returns("non-null", null);
598 }
599 };
600
601 assertEquals("non-null", collaborator.getString());
602 assertNull(collaborator.getString());
603 assertNull(collaborator.getString());
604 }
605
606
607
608
609
610
611
612 @Test
613 void returnsMultipleValuesFromMethodWithReturnTypeOfObject(@Mocked final Collaborator collaborator) {
614 new Expectations() {
615 {
616 collaborator.getObject();
617 returns(1, 2);
618 returns("test", 'X');
619 }
620 };
621
622 assertEquals(1, collaborator.getObject());
623 assertEquals(2, collaborator.getObject());
624 assertEquals("test", collaborator.getObject());
625 assertEquals('X', collaborator.getObject());
626 }
627
628
629
630
631
632
633
634
635
636
637 @Test
638 void returnsMultipleValuesFromGenericMethod(@Mocked final Callable<Integer> callable) throws Exception {
639 new Expectations() {
640 {
641 callable.call();
642 returns(3, 2, 1);
643 }
644 };
645
646 assertEquals(3, callable.call().intValue());
647 assertEquals(2, callable.call().intValue());
648 assertEquals(1, callable.call().intValue());
649 }
650
651
652
653
654
655
656
657 @Test
658 void recordResultsForCollectionAndListReturningMethodsUsingVarargs(@Mocked final Collaborator mock) {
659 new Expectations() {
660 {
661 mock.getItems();
662 returns(1, "2", 3.0);
663 mock.getListItems();
664 returns("a", true);
665 }
666 };
667
668 Collaborator collaborator = new Collaborator();
669 assertArrayEquals(new Object[] { 1, "2", 3.0 }, collaborator.getItems().toArray());
670 assertArrayEquals(new Object[] { "a", true }, collaborator.getListItems().toArray());
671 }
672
673
674
675
676
677
678
679 @Test
680 void recordResultsForIterableReturningMethodUsingVarargs(@Mocked final Collaborator mock) {
681 new Expectations() {
682 {
683 mock.getIterable();
684 returns(true, "Xyz", 3.6);
685 }
686 };
687
688 int i = 0;
689 Object[] expectedValues = { true, "Xyz", 3.6 };
690
691 for (Object value : mock.getIterable()) {
692 assertEquals(expectedValues[i], value);
693 i++;
694 }
695 }
696
697
698
699
700
701
702
703 @Test
704 void recordResultsForIteratorReturningMethodUsingVarargs(@Mocked final Collaborator mock) {
705 new Expectations() {
706 {
707 mock.getIterator();
708 returns("ab", "cde", 1, 3);
709 }
710 };
711
712 Iterator<?> itr = new Collaborator().getIterator();
713 assertEquals("ab", itr.next());
714 assertEquals("cde", itr.next());
715 assertEquals(1, itr.next());
716 assertEquals(3, itr.next());
717 }
718
719
720
721
722
723
724
725 @Test
726 void recordResultsForSetReturningMethodUsingVarargs(@Mocked final Collaborator mock) {
727 new Expectations() {
728 {
729 mock.getSetItems();
730 returns(4.0, "aB", 2);
731 mock.getSortedSetItems();
732 returns(1, 5, 123);
733 }
734 };
735
736 assertArrayEquals(new Object[] { 4.0, "aB", 2 }, mock.getSetItems().toArray());
737 assertArrayEquals(new Object[] { 1, 5, 123 }, mock.getSortedSetItems().toArray());
738 }
739
740
741
742
743 @Test
744 void recordResultsForArrayReturningMethodsUsingVarargs() {
745 final Collaborator collaborator = new Collaborator();
746
747 new Expectations(collaborator) {
748 {
749 collaborator.getIntArray();
750 returns(1, 2, 3, 4);
751 collaborator.getLongArray();
752 returns(1023, 20234L, 354);
753 collaborator.getByteArray();
754 returns(0, -4, 5);
755 collaborator.getByteWrapperArray();
756 returns(0, -4, 5);
757 collaborator.getCharArray();
758 returns('a', 'B');
759 collaborator.getShortArray();
760 returns(-1, 3, 0);
761 collaborator.getShortWrapperArray();
762 returns(-1, 3, 0);
763 collaborator.getFloatArray();
764 returns(-0.1F, 5.6F, 7);
765 collaborator.getDoubleArray();
766 returns(4.1, 15, -7.0E2);
767 collaborator.getStringArray();
768 returns("aX", null, "B2 m");
769 }
770 };
771
772 assertArrayEquals(new int[] { 1, 2, 3, 4 }, collaborator.getIntArray());
773 assertArrayEquals(new long[] { 1023, 20234, 354 }, collaborator.getLongArray());
774 assertArrayEquals(new byte[] { 0, -4, 5 }, collaborator.getByteArray());
775 assertArrayEquals(new Byte[] { 0, -4, 5 }, collaborator.getByteWrapperArray());
776 assertArrayEquals(new char[] { 'a', 'B' }, collaborator.getCharArray());
777 assertArrayEquals(new short[] { -1, 3, 0 }, collaborator.getShortArray());
778 assertArrayEquals(new Short[] { -1, 3, 0 }, collaborator.getShortWrapperArray());
779 assertArrayEquals(new float[] { -0.1F, 5.6F, 7 }, collaborator.getFloatArray(), 0.0F);
780 assertArrayEquals(new double[] { 4.0, 15, -7.0001E2 }, collaborator.getDoubleArray(), 0.1);
781 assertArrayEquals(new String[] { "aX", null, "B2 m" }, collaborator.getStringArray());
782 }
783
784
785
786
787
788
789
790 @Test
791 void recordMultipleIteratorsToBeReturnedFromMethodThatReturnsIterator(@Mocked final Collaborator mock) {
792 final Iterator<?> firstResult = new ArrayList<>().listIterator();
793 final ListIterator<?> secondResult = new LinkedList<>().listIterator();
794 final Iterator<?> thirdResult = new ArrayList<>().iterator();
795
796 new Expectations() {
797 {
798 mock.getListIterator();
799 returns(firstResult, secondResult);
800
801 mock.getIterator();
802 returns(firstResult, secondResult, thirdResult);
803 }
804 };
805
806 assertSame(firstResult, mock.getListIterator());
807 assertSame(secondResult, mock.getListIterator());
808
809 assertSame(firstResult, mock.getIterator());
810 assertSame(secondResult, mock.getIterator());
811 assertSame(thirdResult, mock.getIterator());
812 }
813
814
815
816
817
818
819
820 @Test
821 void recordMultipleListsToBeReturnedFromMethodThatReturnsList(@Mocked final Collaborator mock) {
822 final List<?> firstResult = new ArrayList<>();
823 final List<?> secondResult = new ArrayList<>();
824 final List<?> thirdResult = new LinkedList<>();
825
826 new Expectations() {
827 {
828 mock.getAList();
829 returns(firstResult, secondResult);
830
831 mock.getListItems();
832 returns(firstResult, secondResult, thirdResult);
833 }
834 };
835
836 assertSame(firstResult, mock.getAList());
837 assertSame(secondResult, mock.getAList());
838
839 assertSame(firstResult, mock.getListItems());
840 assertSame(secondResult, mock.getListItems());
841 assertSame(thirdResult, mock.getListItems());
842 }
843 }