1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNotNull;
5 import static org.junit.jupiter.api.Assertions.assertNull;
6
7 import org.junit.jupiter.api.BeforeEach;
8 import org.junit.jupiter.api.Test;
9
10
11
12
13 final class ClassInitializationTest {
14
15 static final class ClassWhichFailsAtInitialization {
16 static {
17
18 if (true) {
19 throw new AssertionError();
20 }
21 }
22
23 static int value() {
24 return 0;
25 }
26 }
27
28 @Test
29 public void usingExpectations(@Mocked(stubOutClassInitialization = true) ClassWhichFailsAtInitialization unused) {
30 new Expectations() {
31 {
32 ClassWhichFailsAtInitialization.value();
33 result = 1;
34 }
35 };
36
37 assertEquals(1, ClassWhichFailsAtInitialization.value());
38 }
39
40
41
42
43 static class ClassWithStaticInitializer {
44
45
46 static final Object CONSTANT = "not a compile-time constant";
47
48
49 static String variable;
50 static {
51 variable = doSomething();
52 }
53
54
55
56
57
58
59 static String doSomething() {
60 return "real value";
61 }
62 }
63
64
65
66
67
68
69
70 @Test
71 void mockClassWithStaticInitializerNotStubbedOut(@Mocked ClassWithStaticInitializer mocked) {
72
73 assertNotNull(ClassWithStaticInitializer.CONSTANT);
74 assertNull(ClassWithStaticInitializer.doSomething());
75 assertEquals("real value", ClassWithStaticInitializer.variable);
76 }
77
78 static class AnotherClassWithStaticInitializer {
79 static final Object CONSTANT = "not a compile-time constant";
80 static {
81 doSomething();
82 }
83
84 static void doSomething() {
85 throw new UnsupportedOperationException("must not execute");
86 }
87
88 int getValue() {
89 return -1;
90 }
91 }
92
93 @Test
94 public void mockClassWithStaticInitializerStubbedOut(
95 @Mocked(stubOutClassInitialization = true) AnotherClassWithStaticInitializer mockAnother) {
96
97 assertNull(AnotherClassWithStaticInitializer.CONSTANT);
98 AnotherClassWithStaticInitializer.doSomething();
99 assertEquals(0, mockAnother.getValue());
100 }
101
102
103
104
105 static class ClassWhichCallsStaticMethodFromInitializer {
106 static {
107 String s = someValue();
108 s.length();
109 }
110
111
112
113
114
115
116 static String someValue() {
117 return "some value";
118 }
119 }
120
121
122
123
124
125
126
127 @Test
128 void mockUninitializedClass(@Mocked ClassWhichCallsStaticMethodFromInitializer unused) {
129 assertNull(ClassWhichCallsStaticMethodFromInitializer.someValue());
130 }
131
132
133
134
135 public interface BaseType {
136
137
138
139
140
141 String someValue();
142 }
143
144
145
146
147 static final class NestedImplementationClass implements BaseType {
148 static {
149 new NestedImplementationClass().someValue().length();
150 }
151
152 @Override
153 public String someValue() {
154 return "some value";
155 }
156 }
157
158
159
160
161 @BeforeEach
162 void loadNestedImplementationClass() {
163
164
165 NestedImplementationClass.class.getName();
166 }
167
168
169
170
171
172
173
174 @Test
175 void mockUninitializedImplementationClass(@Capturing BaseType mockBase) {
176 BaseType obj = new NestedImplementationClass();
177
178 assertNull(obj.someValue());
179 }
180
181
182
183
184 static class Dependency {
185
186
187
188
189
190 static Dependency create() {
191 return null;
192 }
193 }
194
195
196
197
198 static class Dependent {
199
200
201 static final Dependency DEPENDENCY = Dependency.create();
202 static {
203 DEPENDENCY.toString();
204 }
205 }
206
207
208
209
210 static class AnotherDependent {
211
212
213 static final Dependency DEPENDENCY = Dependency.create();
214 static {
215 DEPENDENCY.toString();
216 }
217 }
218
219
220 @Mocked
221 Dependency dependency;
222
223
224 @Mocked
225 Dependent dependent;
226
227
228
229
230
231
232
233 @Test
234 void mockAnotherDependentClass(@Mocked AnotherDependent anotherDependent) {
235 assertNotNull(Dependent.DEPENDENCY);
236 assertNotNull(AnotherDependent.DEPENDENCY);
237 }
238
239
240
241
242 public interface BaseInterface {
243
244 Object DO_NOT_REMOVE = "Testing";
245 }
246
247
248
249
250 public interface SubInterface extends BaseInterface {
251 }
252
253
254 @Mocked
255 SubInterface mock;
256
257
258
259
260 @Test
261 void verifyClassInitializerForMockedBaseInterface() {
262 assertNotNull(mock);
263 assertEquals("Testing", BaseInterface.DO_NOT_REMOVE);
264 }
265
266
267
268
269 static final class ClassWhichCallsMethodOnItselfFromInitializer {
270
271
272 static final Integer value = value();
273
274
275
276
277
278
279 static Integer value() {
280 return null;
281 }
282 }
283
284
285
286
287
288
289
290 @Test
291 void mockClassWhichCallsMethodOnItselfFromInitializerWithoutStubbingOutTheInitializer(
292 @Mocked ClassWhichCallsMethodOnItselfFromInitializer unused) {
293 assertNotNull(ClassWhichCallsMethodOnItselfFromInitializer.value());
294 assertNull(ClassWhichCallsMethodOnItselfFromInitializer.value);
295 }
296
297
298
299
300 interface InterfaceWithStaticInitializer {
301
302 Object CONSTANT = "test";
303 }
304
305
306
307
308 @SuppressWarnings({ "AbstractClassWithoutAbstractMethods", "StaticInheritance" })
309 public abstract static class AbstractImpl implements InterfaceWithStaticInitializer {
310 }
311
312
313
314
315
316
317
318
319 @Test
320 void mockAbstractClassImplementingInterfaceWithStaticInitializer(@Mocked AbstractImpl mock2) {
321 assertEquals("test", InterfaceWithStaticInitializer.CONSTANT);
322 }
323 }