View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
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   * The Class ExpectationsWithValuesToReturnTest.
35   */
36  @ExtendWith(JMockitExtension.class)
37  class ExpectationsWithValuesToReturnTest {
38  
39      /**
40       * The Class Collaborator.
41       */
42      static class Collaborator {
43  
44          /**
45           * Do internal.
46           *
47           * @return the string
48           */
49          static String doInternal() {
50              return "123";
51          }
52  
53          /**
54           * Gets the value.
55           *
56           * @return the value
57           */
58          int getValue() {
59              return -1;
60          }
61  
62          /**
63           * Gets the integer.
64           *
65           * @return the integer
66           */
67          Integer getInteger() {
68              return -1;
69          }
70  
71          /**
72           * Gets the byte value.
73           *
74           * @return the byte value
75           */
76          byte getByteValue() {
77              return -1;
78          }
79  
80          /**
81           * Gets the byte wrapper.
82           *
83           * @return the byte wrapper
84           */
85          Byte getByteWrapper() {
86              return -1;
87          }
88  
89          /**
90           * Gets the short value.
91           *
92           * @return the short value
93           */
94          short getShortValue() {
95              return -1;
96          }
97  
98          /**
99           * Gets the short wrapper.
100          *
101          * @return the short wrapper
102          */
103         Short getShortWrapper() {
104             return -1;
105         }
106 
107         /**
108          * Gets the long value.
109          *
110          * @return the long value
111          */
112         long getLongValue() {
113             return -1;
114         }
115 
116         /**
117          * Gets the long wrapper.
118          *
119          * @return the long wrapper
120          */
121         Long getLongWrapper() {
122             return -1L;
123         }
124 
125         /**
126          * Gets the float value.
127          *
128          * @return the float value
129          */
130         float getFloatValue() {
131             return -1.0F;
132         }
133 
134         /**
135          * Gets the float wrapper.
136          *
137          * @return the float wrapper
138          */
139         Float getFloatWrapper() {
140             return -1.0F;
141         }
142 
143         /**
144          * Gets the double value.
145          *
146          * @return the double value
147          */
148         double getDoubleValue() {
149             return -1.0;
150         }
151 
152         /**
153          * Gets the double wrapper.
154          *
155          * @return the double wrapper
156          */
157         Double getDoubleWrapper() {
158             return -1.0;
159         }
160 
161         /**
162          * Gets the char value.
163          *
164          * @return the char value
165          */
166         char getCharValue() {
167             return '1';
168         }
169 
170         /**
171          * Gets the character.
172          *
173          * @return the character
174          */
175         Character getCharacter() {
176             return '1';
177         }
178 
179         /**
180          * Gets the boolean value.
181          *
182          * @return the boolean value
183          */
184         boolean getBooleanValue() {
185             return true;
186         }
187 
188         /**
189          * Gets the boolean wrapper.
190          *
191          * @return the boolean wrapper
192          */
193         Boolean getBooleanWrapper() {
194             return true;
195         }
196 
197         /**
198          * Gets the string.
199          *
200          * @return the string
201          */
202         String getString() {
203             return "";
204         }
205 
206         /**
207          * Gets the object.
208          *
209          * @return the object
210          */
211         Object getObject() {
212             return null;
213         }
214 
215         /**
216          * Gets the items.
217          *
218          * @return the items
219          */
220         Collection<?> getItems() {
221             return null;
222         }
223 
224         /**
225          * Gets the list items.
226          *
227          * @return the list items
228          */
229         List<?> getListItems() {
230             return null;
231         }
232 
233         /**
234          * Gets the a list.
235          *
236          * @return the a list
237          */
238         List<?> getAList() {
239             return null;
240         }
241 
242         /**
243          * Gets the sets the items.
244          *
245          * @return the sets the items
246          */
247         Set<?> getSetItems() {
248             return null;
249         }
250 
251         /**
252          * Gets the sorted set items.
253          *
254          * @return the sorted set items
255          */
256         SortedSet<?> getSortedSetItems() {
257             return null;
258         }
259 
260         /**
261          * Gets the map items.
262          *
263          * @return the map items
264          */
265         Map<?, ?> getMapItems() {
266             return null;
267         }
268 
269         /**
270          * Gets the sorted map items.
271          *
272          * @return the sorted map items
273          */
274         SortedMap<?, ?> getSortedMapItems() {
275             return null;
276         }
277 
278         /**
279          * Gets the list iterator.
280          *
281          * @return the list iterator
282          */
283         ListIterator<?> getListIterator() {
284             return null;
285         }
286 
287         /**
288          * Gets the iterator.
289          *
290          * @return the iterator
291          */
292         Iterator<?> getIterator() {
293             return null;
294         }
295 
296         /**
297          * Gets the iterable.
298          *
299          * @return the iterable
300          */
301         Iterable<?> getIterable() {
302             return null;
303         }
304 
305         /**
306          * Gets the int array.
307          *
308          * @return the int array
309          */
310         int[] getIntArray() {
311             return null;
312         }
313 
314         /**
315          * Gets the int 2 array.
316          *
317          * @return the int 2 array
318          */
319         int[][] getInt2Array() {
320             return null;
321         }
322 
323         /**
324          * Gets the byte array.
325          *
326          * @return the byte array
327          */
328         byte[] getByteArray() {
329             return null;
330         }
331 
332         /**
333          * Gets the byte wrapper array.
334          *
335          * @return the byte wrapper array
336          */
337         Byte[] getByteWrapperArray() {
338             return null;
339         }
340 
341         /**
342          * Gets the short array.
343          *
344          * @return the short array
345          */
346         short[] getShortArray() {
347             return null;
348         }
349 
350         /**
351          * Gets the short wrapper array.
352          *
353          * @return the short wrapper array
354          */
355         Short[] getShortWrapperArray() {
356             return null;
357         }
358 
359         /**
360          * Gets the long array.
361          *
362          * @return the long array
363          */
364         long[] getLongArray() {
365             return null;
366         }
367 
368         /**
369          * Gets the long 2 array.
370          *
371          * @return the long 2 array
372          */
373         long[][] getLong2Array() {
374             return null;
375         }
376 
377         /**
378          * Gets the float array.
379          *
380          * @return the float array
381          */
382         float[] getFloatArray() {
383             return null;
384         }
385 
386         /**
387          * Gets the double array.
388          *
389          * @return the double array
390          */
391         double[] getDoubleArray() {
392             return null;
393         }
394 
395         /**
396          * Gets the char array.
397          *
398          * @return the char array
399          */
400         char[] getCharArray() {
401             return null;
402         }
403 
404         /**
405          * Gets the boolean array.
406          *
407          * @return the boolean array
408          */
409         boolean[] getBooleanArray() {
410             return null;
411         }
412 
413         /**
414          * Gets the string array.
415          *
416          * @return the string array
417          */
418         String[] getStringArray() {
419             return null;
420         }
421 
422         /**
423          * Gets the string 2 array.
424          *
425          * @return the string 2 array
426          */
427         String[][] getString2Array() {
428             return null;
429         }
430     }
431 
432     /**
433      * Returns default values for primitive and wrapper return types.
434      *
435      * @param mock
436      *            the mock
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      * Returns default values for collection valued return types.
483      *
484      * @param mock
485      *            the mock
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      * Returns default values for array valued return types.
510      *
511      * @param mock
512      *            the mock
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      * Returns multiple values in sequence using varargs.
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      * Returns non null value followed by null using varargs.
587      *
588      * @param collaborator
589      *            the collaborator
590      */
591     @Test
592     void returnsNonNullValueFollowedByNullUsingVarargs(@Mocked final Collaborator collaborator) {
593         new Expectations() {
594             {
595                 collaborator.getString();
596                 // noinspection NullArgumentToVariableArgMethod
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      * Returns multiple values from method with return type of object.
608      *
609      * @param collaborator
610      *            the collaborator
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      * Returns multiple values from generic method.
630      *
631      * @param callable
632      *            the callable
633      *
634      * @throws Exception
635      *             the exception
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      * Record results for collection and list returning methods using varargs.
653      *
654      * @param mock
655      *            the mock
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      * Record results for iterable returning method using varargs.
675      *
676      * @param mock
677      *            the mock
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      * Record results for iterator returning method using varargs.
699      *
700      * @param mock
701      *            the mock
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      * Record results for set returning method using varargs.
721      *
722      * @param mock
723      *            the mock
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      * Record results for array returning methods using varargs.
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      * Record multiple iterators to be returned from method that returns iterator.
786      *
787      * @param mock
788      *            the mock
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      * Record multiple lists to be returned from method that returns list.
816      *
817      * @param mock
818      *            the mock
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 }