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