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