1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNull;
5 import static org.junit.jupiter.api.Assertions.assertSame;
6 import static org.junit.jupiter.api.Assertions.assertTrue;
7
8 import java.io.ByteArrayInputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.net.URL;
12 import java.util.Scanner;
13
14 import org.junit.jupiter.api.BeforeAll;
15 import org.junit.jupiter.api.BeforeEach;
16 import org.junit.jupiter.api.Test;
17
18
19
20
21 final class FakeClassInstantiationPerSetupTest {
22
23
24
25
26 public static final class RealClass1 {
27
28
29
30
31 public static void doSomething() {
32 throw new RuntimeException();
33 }
34
35
36
37
38
39
40
41
42
43
44
45 public int performComputation(int a, boolean b) {
46 return b ? a : -a;
47 }
48 }
49
50
51
52
53 public static final class RealClass2 {
54
55
56
57
58 public static void doSomething() {
59 throw new RuntimeException();
60 }
61
62
63
64
65
66
67
68
69
70
71
72 public int performComputation(int a, boolean b) {
73 return b ? a : -a;
74 }
75 }
76
77
78
79
80 public static final class RealClass3 {
81
82
83
84
85 public static void doSomething() {
86 throw new RuntimeException();
87 }
88
89
90
91
92
93
94
95
96
97
98
99 public int performComputation(int a, boolean b) {
100 return b ? a : -a;
101 }
102 }
103
104
105
106
107 public static final class RealClass4 {
108
109
110
111
112 public static void doSomething() {
113 throw new RuntimeException();
114 }
115
116
117
118
119
120
121
122
123
124
125
126 public int performComputation(int a, boolean b) {
127 return b ? a : -a;
128 }
129 }
130
131
132
133
134 static final class FakeClass1 extends MockUp<RealClass1> {
135
136
137 static Object singleInstanceCreated;
138
139
140
141
142 FakeClass1() {
143 assertNull(singleInstanceCreated);
144 singleInstanceCreated = this;
145 }
146
147
148
149
150 @Mock
151 void doSomething() {
152 assertSame(singleInstanceCreated, this);
153 }
154
155
156
157
158
159
160
161
162
163
164
165 @Mock
166 int performComputation(int a, boolean b) {
167 assertSame(singleInstanceCreated, this);
168 assertTrue(a > 0);
169 assertTrue(b);
170 return 2;
171 }
172 }
173
174
175
176
177 static final class FakeClass2 extends MockUp<RealClass2> {
178
179
180 static Object singleInstanceCreated;
181
182
183
184
185 FakeClass2() {
186 assertNull(singleInstanceCreated);
187 singleInstanceCreated = this;
188 }
189
190
191
192
193 @Mock
194 void doSomething() {
195 assertSame(singleInstanceCreated, this);
196 }
197
198
199
200
201
202
203
204
205
206
207
208 @Mock
209 int performComputation(int a, boolean b) {
210 assertSame(singleInstanceCreated, this);
211 assertTrue(a > 0);
212 assertTrue(b);
213 return 2;
214 }
215 }
216
217
218
219
220 static final class FakeClass3 extends MockUp<RealClass3> {
221
222
223 static Object singleInstanceCreated;
224
225
226
227
228 FakeClass3() {
229 assertNull(singleInstanceCreated);
230 singleInstanceCreated = this;
231 }
232
233
234
235
236 @Mock
237 void doSomething() {
238 assertSame(singleInstanceCreated, this);
239 }
240
241
242
243
244
245
246
247
248
249
250
251 @Mock
252 int performComputation(int a, boolean b) {
253 assertSame(singleInstanceCreated, this);
254 assertTrue(a > 0);
255 assertTrue(b);
256 return 2;
257 }
258 }
259
260
261
262
263 static final class FakeClass4 extends MockUp<RealClass4> {
264
265
266 static Object singleInstanceCreated;
267
268
269
270
271 FakeClass4() {
272 assertNull(singleInstanceCreated);
273 singleInstanceCreated = this;
274 }
275
276
277
278
279 @Mock
280 void doSomething() {
281 assertSame(singleInstanceCreated, this);
282 }
283
284
285
286
287
288
289
290
291
292
293
294 @Mock
295 int performComputation(int a, boolean b) {
296 assertSame(singleInstanceCreated, this);
297 assertTrue(a > 0);
298 assertTrue(b);
299 return 2;
300 }
301 }
302
303
304
305
306 @BeforeAll
307 static void setUpClassLevelFakes() {
308 new FakeClass1();
309 }
310
311
312
313
314 @BeforeAll
315 static void setUpAdditionalClassLevelFakes() {
316 new FakeClass2();
317 }
318
319
320
321
322 @BeforeEach
323 void setUpMethodLevelFakes() {
324 FakeClass3.singleInstanceCreated = null;
325 new FakeClass3();
326 }
327
328
329
330
331 @Test
332 void fakeInstancePerSetupInClassAndFixtureScopes() {
333 assertFakeClass1();
334 assertFakeClass2();
335 assertFakeClass3();
336 assertEquals(1, new RealClass4().performComputation(1, true));
337 }
338
339
340
341
342 void assertFakeClass1() {
343 RealClass1.doSomething();
344 assertEquals(2, new RealClass1().performComputation(1, true));
345 }
346
347
348
349
350 void assertFakeClass2() {
351 RealClass2.doSomething();
352 assertEquals(2, new RealClass2().performComputation(1, true));
353 }
354
355
356
357
358 void assertFakeClass3() {
359 RealClass3.doSomething();
360 assertEquals(2, new RealClass3().performComputation(1, true));
361 }
362
363
364
365
366 void assertFakeClass4() {
367 RealClass4.doSomething();
368 assertEquals(2, new RealClass4().performComputation(1, true));
369 }
370
371
372
373
374 @Test
375 void fakeInstancePerSetupInAllScopes() {
376 new FakeClass4();
377
378 assertFakeClass1();
379 assertFakeClass2();
380 assertFakeClass3();
381 assertFakeClass4();
382 }
383
384
385
386
387 public static final class FakeURL extends MockUp<URL> {
388
389
390
391
392
393
394
395
396
397
398
399
400 @Mock
401 public InputStream openStream(Invocation inv) throws IOException {
402 URL it = inv.getInvokedInstance();
403
404 if ("test".equals(it.getHost())) {
405 return new ByteArrayInputStream("response".getBytes());
406 }
407
408 return it.openStream();
409 }
410 }
411
412
413
414
415
416
417
418 @Test
419 void reentrantFakeForJREClass() throws Exception {
420 new FakeURL();
421
422 InputStream response = new URL("http://test").openStream();
423
424 assertEquals("response", new Scanner(response).nextLine());
425 }
426 }