1 package otherTests;
2
3 import static org.junit.jupiter.api.Assertions.assertFalse;
4
5 import mockit.Capturing;
6 import mockit.Expectations;
7 import mockit.Mocked;
8
9 import org.junit.jupiter.api.BeforeEach;
10 import org.junit.jupiter.api.MethodOrderer.MethodName;
11 import org.junit.jupiter.api.Test;
12 import org.junit.jupiter.api.TestMethodOrder;
13
14
15
16
17 @TestMethodOrder(MethodName.class)
18 final class SubclassTest {
19
20
21 private static boolean superClassConstructorCalled;
22
23
24 private static boolean subClassConstructorCalled;
25
26
27
28
29 public static class SuperClass {
30
31
32 final String name;
33
34
35
36
37
38
39
40
41
42 public SuperClass(int x, String name) {
43 this.name = name + x;
44 superClassConstructorCalled = true;
45 }
46 }
47
48
49
50
51 public static class SubClass extends SuperClass {
52
53
54
55
56
57
58
59 public SubClass(String name) {
60 super(name.length(), name);
61 subClassConstructorCalled = true;
62 }
63 }
64
65
66
67
68 @BeforeEach
69 void setUp() {
70 superClassConstructorCalled = false;
71 subClassConstructorCalled = false;
72 }
73
74
75
76
77
78
79
80 @Test
81 void captureSubclassThroughClassfileTransformer(@Capturing SuperClass captured) {
82 new SubClass("capture");
83
84 assertFalse(superClassConstructorCalled);
85 assertFalse(subClassConstructorCalled);
86 }
87
88
89
90
91
92
93
94 @Test
95 void captureSubclassThroughRedefinitionOfPreviouslyLoadedClasses(@Capturing SuperClass captured) {
96 new SubClass("capture");
97
98 assertFalse(superClassConstructorCalled);
99 assertFalse(subClassConstructorCalled);
100 }
101
102
103
104
105
106
107
108 @Test
109 void mockSubclassUsingExpectationsWithFirstSuperConstructor(@Mocked SubClass mock) {
110 new Expectations() {
111 {
112 new SubClass("test");
113 }
114 };
115
116 new SubClass("test");
117
118 assertFalse(superClassConstructorCalled);
119 assertFalse(subClassConstructorCalled);
120 }
121 }