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