1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
11 import static org.junit.jupiter.api.Assertions.assertNotSame;
12 import static org.junit.jupiter.api.Assertions.assertNull;
13 import static org.junit.jupiter.api.Assertions.assertSame;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16 import static org.junit.jupiter.api.Assertions.fail;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.lang.management.CompilationMXBean;
23 import java.lang.management.ManagementFactory;
24 import java.net.InetAddress;
25 import java.net.InetSocketAddress;
26 import java.net.Socket;
27 import java.net.SocketAddress;
28 import java.nio.channels.SocketChannel;
29 import java.nio.file.Path;
30 import java.util.Arrays;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.concurrent.Future;
36
37 import mockit.integration.junit5.JMockitExtension;
38 import mockit.internal.expectations.invocation.MissingInvocation;
39
40 import org.junit.jupiter.api.Disabled;
41 import org.junit.jupiter.api.MethodOrderer.MethodName;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.TestMethodOrder;
44 import org.junit.jupiter.api.extension.ExtendWith;
45
46
47
48
49 @ExtendWith(JMockitExtension.class)
50 @SuppressWarnings("ConstantConditions")
51 @TestMethodOrder(MethodName.class)
52 class CascadingParametersTest {
53
54
55
56
57 static class Foo {
58
59
60
61
62
63
64 Bar getBar() {
65 return null;
66 }
67
68
69
70
71
72
73 static Bar globalBar() {
74 return null;
75 }
76
77
78
79
80
81
82
83 void doSomething(String s) {
84 throw new RuntimeException(s);
85 }
86
87
88
89
90
91
92 int getIntValue() {
93 return 1;
94 }
95
96
97
98
99
100
101 Boolean getBooleanValue() {
102 return true;
103 }
104
105
106
107
108
109
110 final List<Integer> getList() {
111 return null;
112 }
113
114
115
116
117
118
119 HashMap<?, ?> getMap() {
120 return null;
121 }
122 }
123
124
125
126
127 static class Bar {
128
129
130
131
132 Bar() {
133 throw new RuntimeException();
134 }
135
136
137
138
139
140
141 int doSomething() {
142 return 1;
143 }
144
145
146
147
148
149
150 Baz getBaz() {
151 return null;
152 }
153
154
155
156
157
158
159
160
161
162 Baz getBaz(@SuppressWarnings("unused") int i) {
163 return null;
164 }
165
166
167
168
169
170
171 AnEnum getEnum() {
172 return null;
173 }
174
175
176
177
178
179
180 static String staticMethod() {
181 return "notMocked";
182 }
183 }
184
185
186
187
188 static final class SubBar extends Bar {
189 }
190
191
192
193
194 public interface Baz {
195
196
197
198
199 void runIt();
200
201
202
203
204
205
206 Date getDate();
207 }
208
209
210
211
212 enum AnEnum {
213
214 First,
215
216 Second,
217
218 Third
219 }
220
221
222 static Bar cascadedBar1;
223
224
225 static Bar cascadedBar2;
226
227
228
229
230
231
232
233 @Test
234 void cascadeOneLevelDuringReplay(@Mocked Foo foo) {
235 cascadedBar1 = foo.getBar();
236 assertEquals(0, cascadedBar1.doSomething());
237
238 cascadedBar2 = Foo.globalBar();
239 assertEquals(0, cascadedBar2.doSomething());
240
241 Bar bar = foo.getBar();
242 assertSame(cascadedBar1, bar);
243
244 Bar globalBar = Foo.globalBar();
245 assertSame(cascadedBar2, globalBar);
246 assertNotSame(bar, globalBar);
247
248 foo.doSomething("test");
249 assertEquals(0, foo.getIntValue());
250 assertFalse(foo.getBooleanValue());
251 assertTrue(foo.getList().isEmpty());
252
253 Map<?, ?> map = foo.getMap();
254 assertNull(map);
255 }
256
257
258
259
260
261
262
263 @Test
264 void verifyThatPreviousCascadedInstancesHaveBeenDiscarded(@Mocked Foo foo) {
265 Bar bar = foo.getBar();
266 assertNotSame(cascadedBar1, bar);
267
268 Bar globalBar = Foo.globalBar();
269 assertNotSame(cascadedBar2, globalBar);
270 }
271
272
273
274
275
276
277
278 @Test
279 void verifyThatStaticMethodsAndConstructorsAreNotMockedWhenCascading(@Mocked Foo foo) {
280 foo.getBar();
281
282 assertEquals("notMocked", Bar.staticMethod());
283
284 try {
285 new Bar();
286 fail();
287 } catch (RuntimeException ignored) {
288 }
289 }
290
291
292
293
294
295
296
297
298
299 @Test
300 void verifyThatStaticMethodsAndConstructorsAreMockedWhenCascadedMockIsMockedNormally(@Mocked Foo mockFoo,
301 @Mocked Bar mockBar) {
302 assertSame(mockBar, mockFoo.getBar());
303 assertEquals(0, mockBar.doSomething());
304 assertNull(Bar.staticMethod());
305 new Bar();
306 }
307
308
309
310
311
312
313
314
315
316 @Test
317 void useAvailableMockedInstanceOfSubclassAsCascadedInstance(@Mocked Foo foo, @Mocked SubBar bar) {
318 Bar cascadedBar = foo.getBar();
319
320 assertSame(bar, cascadedBar);
321 }
322
323
324
325
326
327
328
329
330
331
332
333 @Test
334 void replaceCascadedInstanceWithFirstOneOfTwoInjectableInstances(@Mocked final Foo foo, @Injectable final Bar bar1,
335 @Injectable Bar bar2) {
336 new Expectations() {
337 {
338 foo.getBar();
339 result = bar1;
340 }
341 };
342
343 Bar cascadedBar = foo.getBar();
344
345 assertSame(bar1, cascadedBar);
346 assertEquals(0, bar1.doSomething());
347 assertEquals(0, bar2.doSomething());
348 }
349
350
351
352
353
354
355
356 @Test
357 void cascadeOneLevelDuringRecord(@Mocked final Foo mockFoo) {
358 final List<Integer> list = Arrays.asList(1, 2, 3);
359
360 new Expectations() {
361 {
362 mockFoo.doSomething(anyString);
363 minTimes = 2;
364 mockFoo.getBar().doSomething();
365 result = 2;
366 Foo.globalBar().doSomething();
367 result = 3;
368 mockFoo.getBooleanValue();
369 result = true;
370 mockFoo.getIntValue();
371 result = -1;
372 mockFoo.getList();
373 result = list;
374 }
375 };
376
377 Foo foo = new Foo();
378 foo.doSomething("1");
379 assertEquals(2, foo.getBar().doSomething());
380 foo.doSomething("2");
381 assertEquals(3, Foo.globalBar().doSomething());
382 assertTrue(foo.getBooleanValue());
383 assertEquals(-1, foo.getIntValue());
384 assertSame(list, foo.getList());
385 }
386
387
388
389
390
391
392
393 @Test
394 void cascadeOneLevelDuringVerify(@Mocked final Foo foo) {
395 Bar bar = foo.getBar();
396 bar.doSomething();
397 bar.doSomething();
398
399 Foo.globalBar().doSomething();
400
401 assertEquals(0, foo.getIntValue());
402 assertFalse(foo.getBooleanValue());
403
404 assertTrue(foo.getList().isEmpty());
405
406 new Verifications() {
407 {
408 foo.getBar().doSomething();
409 minTimes = 2;
410 Foo.globalBar().doSomething();
411 times = 1;
412 }
413 };
414
415 new VerificationsInOrder() {
416 {
417 foo.getIntValue();
418 foo.getBooleanValue();
419 }
420 };
421 }
422
423
424
425
426
427
428
429 @Test
430 void cascadeTwoLevelsDuringReplay(@Mocked Foo foo) {
431 foo.getBar().getBaz().runIt();
432 }
433
434
435
436
437
438
439
440 @Test
441 void cascadeTwoLevelsDuringRecord(@Mocked final Foo mockFoo) {
442 new Expectations() {
443 {
444 mockFoo.getBar().doSomething();
445 result = 1;
446 Foo.globalBar().doSomething();
447 result = 2;
448
449 mockFoo.getBar().getBaz().runIt();
450 times = 2;
451 }
452 };
453
454 Foo foo = new Foo();
455 assertEquals(1, foo.getBar().doSomething());
456 assertEquals(2, Foo.globalBar().doSomething());
457
458 Baz baz = foo.getBar().getBaz();
459 baz.runIt();
460 baz.runIt();
461 }
462
463
464
465
466
467
468
469
470
471 @Test
472 void cascadeOneLevelAndVerifyInvocationOnLastMockOnly(@Mocked Foo foo, @Injectable final Bar bar) {
473 Bar fooBar = foo.getBar();
474 assertSame(bar, fooBar);
475 fooBar.doSomething();
476
477 new Verifications() {
478 {
479 bar.doSomething();
480 }
481 };
482 }
483
484
485
486
487
488
489
490
491
492 @Test
493 void cascadeTwoLevelsWithInvocationRecordedOnLastMockOnly(@Mocked Foo foo, @Mocked final Baz baz) {
494 new Expectations() {
495 {
496 baz.runIt();
497 times = 1;
498 }
499 };
500
501 Baz cascadedBaz = foo.getBar().getBaz();
502 cascadedBaz.runIt();
503 }
504
505
506
507
508
509
510
511
512
513 @Test
514 void cascadeTwoLevelsAndVerifyInvocationOnLastMockOnly(@Mocked Foo foo, @Mocked final Baz baz) {
515 Baz cascadedBaz = foo.getBar().getBaz();
516 assertSame(baz, cascadedBaz);
517 cascadedBaz.runIt();
518
519 new Verifications() {
520 {
521 baz.runIt();
522 }
523 };
524 }
525
526
527
528
529
530
531
532
533
534
535
536
537 @Test
538 void cascadeOnJREClasses(@Mocked final ProcessBuilder pb) throws Exception {
539 new Expectations() {
540 {
541 ProcessBuilder sameBuilder = pb.directory((File) any);
542 assertSame(sameBuilder, pb);
543
544 Process process = sameBuilder.start();
545 process.getOutputStream().write(5);
546 process.exitValue();
547 result = 1;
548 }
549 };
550
551 Process process = new ProcessBuilder("test").directory(Path.of("myDir").toFile()).start();
552 process.getOutputStream().write(5);
553 process.getOutputStream().flush();
554 assertEquals(1, process.exitValue());
555 }
556
557
558
559
560
561
562
563
564
565 @Test
566 void returnSameMockedInstanceThroughCascadingEvenWithMultipleCandidatesAvailable(@Injectable ProcessBuilder pb1,
567 @Injectable ProcessBuilder pb2) {
568 assertSame(pb1, pb1.command("a"));
569 assertSame(pb2, pb2.command("b"));
570 }
571
572
573
574
575
576
577
578
579
580
581 @Test
582 void createOSProcessToCopyTempFiles(@Mocked final ProcessBuilder pb) throws Exception {
583
584 String cmdLine = "copy /Y *.txt D:\\TEMP";
585 File wrkDir = Path.of("C:\\TEMP").toFile();
586 Process copy = new ProcessBuilder().command(cmdLine).directory(wrkDir).start();
587 int exit = copy.waitFor();
588
589 if (exit != 0) {
590 throw new RuntimeException("Process execution failed");
591 }
592
593
594 new Verifications() {
595 {
596 pb.command(withSubstring("copy")).start();
597 }
598 };
599 }
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617 @Disabled
618 @Test
619 void recordAndVerifyExpectationsOnCascadedMocks(@Mocked Socket anySocket,
620 @Mocked final SocketChannel cascadedChannel, @Mocked InetSocketAddress inetAddr) throws Exception {
621 Socket sk = new Socket();
622 SocketChannel ch = sk.getChannel();
623
624 if (!ch.isConnected()) {
625 SocketAddress sa = new InetSocketAddress("remoteHost", 123);
626 ch.connect(sa);
627 }
628
629 InetAddress adr1 = sk.getInetAddress();
630 InetAddress adr2 = sk.getLocalAddress();
631 assertNotSame(adr1, adr2);
632 sk.close();
633
634 new Verifications() {
635 {
636 cascadedChannel.connect((SocketAddress) withNotNull());
637 }
638 };
639 }
640
641
642
643
644 static final class SocketFactory {
645
646
647
648
649
650
651 public Socket createSocket() {
652 return new Socket();
653 }
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668 public Socket createSocket(String host, int port) throws IOException {
669 return new Socket(host, port);
670 }
671 }
672
673
674
675
676
677
678
679
680
681
682 @Test
683 void cascadeOneLevelWithArgumentMatchers(@Mocked final SocketFactory sf) throws Exception {
684 new Expectations() {
685 {
686 sf.createSocket(anyString, 80);
687 result = null;
688 }
689 };
690
691 assertNull(sf.createSocket("expected", 80));
692 assertNotNull(sf.createSocket("unexpected", 8080));
693 }
694
695
696
697
698
699
700
701
702
703
704 @Test
705 void recordAndVerifyOneLevelDeep(@Mocked final SocketFactory sf) throws Exception {
706 final OutputStream out = new ByteArrayOutputStream();
707
708 new Expectations() {
709 {
710 sf.createSocket().getOutputStream();
711 result = out;
712 }
713 };
714
715 assertSame(out, sf.createSocket().getOutputStream());
716 }
717
718
719
720
721
722
723
724
725
726
727
728
729 @Test
730 void recordAndVerifyOnTwoCascadingMocksOfTheSameType(@Mocked final SocketFactory sf1,
731 @Mocked final SocketFactory sf2) throws Exception {
732 final OutputStream out1 = new ByteArrayOutputStream();
733 final OutputStream out2 = new ByteArrayOutputStream();
734
735 new Expectations() {
736 {
737 sf1.createSocket().getOutputStream();
738 result = out1;
739 sf2.createSocket().getOutputStream();
740 result = out2;
741 }
742 };
743
744 assertSame(out1, sf1.createSocket().getOutputStream());
745 assertSame(out2, sf2.createSocket().getOutputStream());
746
747 new VerificationsInOrder() {
748 {
749 sf1.createSocket().getOutputStream();
750 sf2.createSocket().getOutputStream();
751 }
752 };
753 }
754
755
756
757
758
759
760
761
762
763
764 @Test
765 void recordAndVerifySameInvocationOnMocksReturnedFromInvocationsWithDifferentArguments(
766 @Mocked final SocketFactory sf) throws Exception {
767 new Expectations() {
768 {
769 sf.createSocket().getPort();
770 result = 1;
771 sf.createSocket("first", 80).getPort();
772 result = 2;
773 sf.createSocket("second", 80).getPort();
774 result = 3;
775 sf.createSocket(anyString, 81).getPort();
776 result = 4;
777 }
778 };
779
780 assertEquals(1, sf.createSocket().getPort());
781 assertEquals(2, sf.createSocket("first", 80).getPort());
782 assertEquals(3, sf.createSocket("second", 80).getPort());
783 assertEquals(4, sf.createSocket("third", 81).getPort());
784
785 new VerificationsInOrder() {
786 {
787 sf.createSocket().getPort();
788 times = 1;
789 sf.createSocket("first", 80).getPort();
790 sf.createSocket("second", 80).getPort();
791 sf.createSocket(anyString, 81).getPort();
792 maxTimes = 1;
793 sf.createSocket("fourth", -1);
794 times = 0;
795 }
796 };
797 }
798
799
800
801
802
803
804
805 @Test
806 void cascadeOnInheritedMethod(@Mocked SocketChannel sc) {
807 assertNotNull(sc.provider());
808 }
809
810
811
812
813
814
815
816
817
818
819 @Test
820 void recordAndVerifyWithMixedCascadeLevels(@Mocked final SocketFactory sf) throws Exception {
821 new Expectations() {
822 {
823 sf.createSocket("first", 80).getKeepAlive();
824 result = true;
825 sf.createSocket("second", anyInt).getChannel().close();
826 times = 1;
827 }
828 };
829
830 sf.createSocket("second", 80).getChannel().close();
831 assertTrue(sf.createSocket("first", 80).getKeepAlive());
832 sf.createSocket("first", 8080).getChannel().provider().openPipe();
833
834 new Verifications() {
835 {
836 sf.createSocket("first", 8080).getChannel().provider().openPipe();
837 }
838 };
839 }
840
841
842
843
844
845
846 static class SomeClass {
847
848
849
850
851
852 Future<Foo> doSomething() {
853 return null;
854 }
855 }
856
857
858
859
860
861
862
863
864
865
866 @Test
867 void cascadeAFuture(@Mocked SomeClass mock) throws Exception {
868 Future<Foo> f = mock.doSomething();
869 Foo foo = f.get();
870
871 assertNotNull(foo);
872 }
873
874
875
876
877
878
879
880
881
882
883
884 @Test
885 void recordExpectationOnCascadedMock(@Mocked Foo foo, @Mocked final Bar mockBar) {
886 new Expectations() {
887 {
888 mockBar.doSomething();
889 times = 1;
890 result = 123;
891 }
892 };
893
894 Bar bar = foo.getBar();
895 assertEquals(123, bar.doSomething());
896 }
897
898
899
900
901
902
903
904
905
906
907
908
909
910 @Test
911 void overrideTwoCascadedMocksOfTheSameType(@Mocked final Foo foo1, @Mocked final Foo foo2,
912 @Mocked final Bar mockBar1, @Mocked final Bar mockBar2) {
913 new Expectations() {
914 {
915 foo1.getBar();
916 result = mockBar1;
917 foo2.getBar();
918 result = mockBar2;
919 mockBar1.doSomething();
920 mockBar2.doSomething();
921 }
922 };
923
924 Bar bar1 = foo1.getBar();
925 Bar bar2 = foo2.getBar();
926 bar1.doSomething();
927 bar2.doSomething();
928 }
929
930
931
932
933
934
935
936
937
938
939
940
941
942 @Test
943 void overrideTwoCascadedMocksOfTheSameTypeButReplayInDifferentOrder(@Mocked final Foo foo1, @Mocked final Foo foo2,
944 @Injectable final Bar mockBar1, @Mocked final Bar mockBar2) {
945 assertThrows(MissingInvocation.class, () -> {
946 new Expectations() {
947 {
948 foo1.getBar();
949 result = mockBar1;
950 foo2.getBar();
951 result = mockBar2;
952 }
953 };
954
955 Bar bar1 = foo1.getBar();
956 Bar bar2 = foo2.getBar();
957 bar2.doSomething();
958 bar1.doSomething();
959
960 new VerificationsInOrder() {
961 {
962 mockBar1.doSomething();
963 mockBar2.doSomething();
964 }
965 };
966 });
967 }
968
969
970
971
972
973
974
975 @Test
976 void cascadedEnum(@Mocked final Foo mock) {
977 new Expectations() {
978 {
979 mock.getBar().getEnum();
980 result = AnEnum.Second;
981 }
982 };
983
984 assertEquals(AnEnum.Second, mock.getBar().getEnum());
985 }
986
987
988
989
990
991
992
993 @Test
994 void cascadedEnumReturningConsecutiveValuesThroughResultField(@Mocked final Foo mock) {
995 new Expectations() {
996 {
997 mock.getBar().getEnum();
998 result = AnEnum.First;
999 result = AnEnum.Second;
1000 result = AnEnum.Third;
1001 }
1002 };
1003
1004 assertSame(AnEnum.First, mock.getBar().getEnum());
1005 assertSame(AnEnum.Second, mock.getBar().getEnum());
1006 assertSame(AnEnum.Third, mock.getBar().getEnum());
1007 }
1008
1009
1010
1011
1012
1013
1014
1015 @Test
1016 void cascadedEnumReturningConsecutiveValuesThroughReturnsMethod(@Mocked final Foo mock) {
1017 new Expectations() {
1018 {
1019 mock.getBar().getEnum();
1020 returns(AnEnum.First, AnEnum.Second, AnEnum.Third);
1021 }
1022 };
1023
1024 assertSame(AnEnum.First, mock.getBar().getEnum());
1025 assertSame(AnEnum.Second, mock.getBar().getEnum());
1026 assertSame(AnEnum.Third, mock.getBar().getEnum());
1027 }
1028
1029
1030
1031
1032
1033
1034
1035 @Test
1036 void overrideLastCascadedObjectWithNonMockedInstance(@Mocked final Foo foo) {
1037 final Date newDate = new Date(123);
1038 assertEquals(123, newDate.getTime());
1039
1040 new Expectations() {
1041 {
1042 foo.getBar().getBaz().getDate();
1043 result = newDate;
1044 }
1045 };
1046
1047 assertSame(newDate, new Foo().getBar().getBaz().getDate());
1048 assertEquals(123, newDate.getTime());
1049 }
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059 @Test
1060 void returnDeclaredMockedInstanceFromMultiLevelCascading(@Mocked Date mockedDate, @Mocked Foo foo) {
1061 Date newDate = new Date(123);
1062 assertEquals(0, newDate.getTime());
1063
1064 Date cascadedDate = new Foo().getBar().getBaz().getDate();
1065
1066 assertSame(mockedDate, cascadedDate);
1067 assertEquals(0, newDate.getTime());
1068 assertEquals(0, mockedDate.getTime());
1069 }
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079 @Test
1080 void returnInjectableMockInstanceFromMultiLevelCascading(@Injectable Date mockDate, @Mocked Foo foo) {
1081 Date newDate = new Date(123);
1082 assertEquals(123, newDate.getTime());
1083
1084 Date cascadedDate = new Foo().getBar().getBaz().getDate();
1085
1086 assertSame(mockDate, cascadedDate);
1087 assertEquals(123, newDate.getTime());
1088 assertEquals(0, mockDate.getTime());
1089 }
1090
1091
1092
1093
1094 static class Factory {
1095
1096
1097
1098
1099
1100 static Factory create() {
1101 return null;
1102 }
1103 }
1104
1105
1106
1107
1108 static class Client {
1109
1110
1111
1112
1113
1114 OtherClient getOtherClient() {
1115 return null;
1116 }
1117 }
1118
1119
1120
1121
1122 static class OtherClient {
1123
1124 static final Factory F = Factory.create();
1125 }
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135 @Test
1136 void cascadeDuringStaticInitializationOfCascadingClass(@Mocked Factory mock1, @Mocked Client mock2) {
1137 assertNotNull(mock2.getOtherClient());
1138 assertNotNull(OtherClient.F);
1139 }
1140
1141
1142
1143
1144 public interface LevelZero {
1145
1146
1147
1148
1149
1150 Runnable getFoo();
1151 }
1152
1153
1154
1155
1156 public interface LevelOne extends LevelZero {
1157 }
1158
1159
1160
1161
1162 public interface LevelTwo extends LevelOne {
1163 }
1164
1165
1166
1167
1168
1169
1170
1171 @Test
1172 void createCascadedMockFromMethodDefinedTwoLevelsUpAnInterfaceHierarchy(@Mocked LevelTwo mock) {
1173 assertNotNull(mock.getFoo());
1174 }
1175
1176
1177
1178
1179 public abstract static class AbstractClass implements LevelZero {
1180 }
1181
1182
1183
1184
1185
1186
1187
1188 @Test
1189 void cascadeTypeReturnedFromInterfaceImplementedByAbstractClass(@Mocked AbstractClass mock) {
1190 Runnable foo = mock.getFoo();
1191 assertNotNull(foo);
1192 }
1193
1194
1195
1196
1197
1198
1199
1200 @Test
1201 void produceDifferentCascadedInstancesOfSameInterfaceFromDifferentInvocations(@Mocked Bar bar) {
1202 Baz cascaded1 = bar.getBaz(1);
1203 Baz cascaded2 = bar.getBaz(2);
1204 Baz cascaded3 = bar.getBaz(1);
1205
1206 assertSame(cascaded1, cascaded3);
1207 assertNotSame(cascaded1, cascaded2);
1208 }
1209
1210
1211
1212
1213
1214
1215
1216 @Test
1217 void cascadeFromJavaManagementAPI(@Mocked ManagementFactory mngmntFactory) {
1218 CompilationMXBean compilation = ManagementFactory.getCompilationMXBean();
1219
1220 assertNotNull(compilation);
1221 assertNull(compilation.getName());
1222 }
1223
1224
1225
1226
1227 public interface AnInterface {
1228
1229
1230
1231
1232
1233 NonPublicTestedClass getPackagePrivateClass();
1234 }
1235
1236
1237
1238
1239
1240
1241
1242 @Test
1243 void cascadeFromMethodInPublicInterfaceReturningPackagePrivateType(@Mocked AnInterface mock) {
1244 NonPublicTestedClass ret = mock.getPackagePrivateClass();
1245
1246 assertNull(ret);
1247 }
1248
1249
1250
1251
1252 public static final class CustomException extends Throwable {
1253
1254 private static final long serialVersionUID = 1L;
1255 }
1256
1257
1258
1259
1260 static class AClass {
1261
1262
1263
1264
1265
1266 CustomException getException() {
1267 return new CustomException();
1268 }
1269 }
1270
1271
1272
1273
1274
1275
1276
1277 @Test
1278 void cascadeFromMethodReturningAThrowableSubclass(@Mocked AClass mock) {
1279 CustomException t = mock.getException();
1280
1281 assertNull(t);
1282 }
1283
1284
1285
1286
1287 static class First {
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298 <T extends Second> T getSecond(@SuppressWarnings("unused") Class<T> aClass) {
1299 return null;
1300 }
1301 }
1302
1303
1304
1305
1306 static class Second {
1307
1308
1309
1310
1311
1312 Runnable getSomething() {
1313 return null;
1314 }
1315 }
1316
1317
1318
1319
1320
1321
1322
1323 @Test
1324 void cascadeFromMethodReturningTypeProvidedByClassParameterThenFromCascadedInstance(@Mocked First first) {
1325 Second second = first.getSecond(Second.class);
1326 Runnable runnable = second.getSomething();
1327
1328 assertNotNull(runnable);
1329 }
1330
1331 }