1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertTrue;
5
6 import edu.umd.cs.findbugs.annotations.NonNull;
7
8 import java.io.StringReader;
9 import java.nio.CharBuffer;
10
11 import org.junit.jupiter.api.AfterAll;
12 import org.junit.jupiter.api.AfterEach;
13 import org.junit.jupiter.api.BeforeAll;
14 import org.junit.jupiter.api.BeforeEach;
15 import org.junit.jupiter.api.MethodOrderer.MethodName;
16 import org.junit.jupiter.api.Test;
17 import org.junit.jupiter.api.TestMethodOrder;
18
19
20
21
22 @TestMethodOrder(MethodName.class)
23 final class FakingBaseTypesTest {
24
25
26
27
28 static class BeforeClassBaseAction {
29
30
31
32
33
34 protected int perform() {
35 return -1;
36 }
37 }
38
39
40
41
42 static class BeforeClassAction extends BeforeClassBaseAction {
43 @Override
44 protected int perform() {
45 return 12;
46 }
47 }
48
49
50
51
52
53
54
55 @BeforeAll
56 static <T extends BeforeClassBaseAction> void applyFakeForAllTests() {
57 new MockUp<T>() {
58 @Mock
59 int perform() {
60 return 34;
61 }
62 };
63 }
64
65
66
67
68 @AfterAll
69 static void verifyFakeForAllTestsIsInEffect() {
70 int i1 = new BeforeClassAction().perform();
71 int i2 = new BeforeClassBaseAction().perform();
72
73 assertEquals(34, i1);
74 assertEquals(34, i2);
75 }
76
77
78
79
80 public interface IBeforeAction {
81
82
83
84
85
86 @SuppressWarnings("unused")
87 int perform();
88 }
89
90
91
92
93 static class BeforeAction implements IBeforeAction {
94 @Override
95 public int perform() {
96 return 123;
97 }
98 }
99
100
101
102
103
104
105
106 @BeforeEach
107 <T extends IBeforeAction> void applyFakeForEachTest() {
108 new MockUp<T>() {
109 @Mock
110 int perform() {
111 return 56;
112 }
113 };
114 }
115
116
117
118
119 @AfterEach
120 void verifyFakeForEachTestIsInEffect() {
121 int i = new BeforeAction().perform();
122 assertEquals(56, i);
123 }
124
125
126
127
128 public interface IAction {
129
130
131
132
133
134
135
136
137
138 int perform(int i);
139
140
141
142
143
144
145 boolean notToBeFaked();
146 }
147
148
149
150
151 public static class ActionImpl1 implements IAction {
152 @Override
153 public int perform(int i) {
154 return i - 1;
155 }
156
157 @Override
158 public boolean notToBeFaked() {
159 return true;
160 }
161 }
162
163
164
165
166 static final class ActionImpl2 implements IAction {
167 @Override
168 public int perform(int i) {
169 return i - 2;
170 }
171
172 @Override
173 public boolean notToBeFaked() {
174 return true;
175 }
176 }
177
178
179 IAction actionI;
180
181
182
183
184
185
186
187 @Test
188 <T extends IAction> void test3_fakeInterfaceImplementationClassesUsingAnonymousFake() {
189 actionI = new ActionImpl1();
190
191 new MockUp<T>() {
192 @Mock
193 int perform(int i) {
194 return i + 1;
195 }
196 };
197
198 assertEquals(2, actionI.perform(1));
199 assertTrue(actionI.notToBeFaked());
200
201 ActionImpl2 impl2 = new ActionImpl2();
202 assertEquals(3, impl2.perform(2));
203 assertTrue(impl2.notToBeFaked());
204 }
205
206
207
208
209 public interface TestInterface {
210
211
212
213
214
215 String getData();
216 }
217
218
219
220
221
222
223
224 public static final class FakeTestInterface<T extends TestInterface> extends MockUp<T> {
225
226
227
228
229
230
231
232
233
234 @Mock
235 public String getData(Invocation inv) {
236 return "faked" + inv.proceed();
237 }
238 }
239
240
241
242
243 @Test
244 void fakeAllClassesImplementingAnInterfaceUsingNamedFakeWithInvocationParameter() {
245 TestInterface impl1 = new TestInterface() {
246 @Override
247 public String getData() {
248 return "1";
249 }
250 };
251 TestInterface impl2 = new TestInterface() {
252 @Override
253 public String getData() {
254 return "2";
255 }
256 };
257 new FakeTestInterface();
258
259 String faked1 = impl1.getData();
260 String faked2 = impl2.getData();
261
262 assertEquals("faked1", faked1);
263 assertEquals("faked2", faked2);
264 }
265
266
267
268
269 public abstract static class BaseAction {
270
271
272
273
274
275
276
277
278
279 protected abstract int perform(int i);
280
281
282
283
284
285
286 public int toBeFakedAsWell() {
287 return -1;
288 }
289
290
291
292
293
294
295 int notToBeFaked() {
296 return 1;
297 }
298 }
299
300
301
302
303 static final class ConcreteAction1 extends BaseAction {
304 @Override
305 public int perform(int i) {
306 return i - 1;
307 }
308 }
309
310
311
312
313 static class ConcreteAction2 extends BaseAction {
314 @Override
315 protected int perform(int i) {
316 return i - 2;
317 }
318
319 @Override
320 public int toBeFakedAsWell() {
321 return super.toBeFakedAsWell() - 1;
322 }
323
324 @Override
325 int notToBeFaked() {
326 return super.notToBeFaked() + 1;
327 }
328 }
329
330
331
332
333 static class ConcreteAction3 extends ConcreteAction2 {
334 @Override
335 public int perform(int i) {
336 return i - 3;
337 }
338
339 @Override
340 public int toBeFakedAsWell() {
341 return -3;
342 }
343
344 @Override
345 final int notToBeFaked() {
346 return 3;
347 }
348 }
349
350
351 BaseAction actionB;
352
353
354
355
356
357
358
359 @Test
360 <T extends BaseAction> void test4_fakeConcreteSubclassesUsingAnonymousFake() {
361 actionB = new ConcreteAction1();
362
363 new MockUp<T>() {
364 @Mock
365 int perform(int i) {
366 return i + 1;
367 }
368
369 @Mock
370 int toBeFakedAsWell() {
371 return 123;
372 }
373 };
374
375 assertEquals(2, actionB.perform(1));
376 assertEquals(123, actionB.toBeFakedAsWell());
377 assertEquals(1, actionB.notToBeFaked());
378
379 ConcreteAction2 action2 = new ConcreteAction2();
380 assertEquals(3, action2.perform(2));
381 assertEquals(123, action2.toBeFakedAsWell());
382 assertEquals(2, action2.notToBeFaked());
383
384 ConcreteAction3 action3 = new ConcreteAction3();
385 assertEquals(4, action3.perform(3));
386 assertEquals(123, action3.toBeFakedAsWell());
387 assertEquals(3, action3.notToBeFaked());
388 }
389
390
391
392
393 @AfterEach
394 void checkImplementationClassesAreNoLongerFaked() {
395 if (actionI != null) {
396 assertEquals(-1, actionI.perform(0));
397 }
398
399 assertEquals(-2, new ActionImpl2().perform(0));
400
401 if (actionB != null) {
402 assertEquals(-1, actionB.perform(0));
403 }
404
405 assertEquals(-2, new ConcreteAction2().perform(0));
406 assertEquals(-3, new ConcreteAction3().perform(0));
407 }
408
409
410
411
412
413
414
415 static final class FakeInterface<T extends IAction> extends MockUp<T> {
416
417
418
419
420
421
422
423
424
425 @Mock
426 int perform(int i) {
427 return i + 2;
428 }
429 }
430
431
432
433
434 @Test
435 void test5_fakeInterfaceImplementationClassesUsingNamedFake() {
436 new FakeInterface();
437
438 actionI = new ActionImpl1();
439 assertEquals(3, actionI.perform(1));
440 assertEquals(4, new ActionImpl2().perform(2));
441 }
442
443
444
445
446
447
448
449 static final class FakeBaseClass<T extends BaseAction> extends MockUp<T> {
450
451
452
453
454
455
456
457
458
459 @Mock
460 int perform(int i) {
461 return i + 3;
462 }
463 }
464
465
466
467
468 @Test
469 void test6_fakeConcreteSubclassesUsingNamedFake() {
470 new FakeBaseClass();
471
472 actionB = new ConcreteAction1();
473 assertEquals(4, actionB.perform(1));
474 assertEquals(5, new ConcreteAction2().perform(2));
475 assertEquals(6, new ConcreteAction3().perform(3));
476 }
477
478
479
480
481
482
483
484 interface GenericIAction<N extends Number> {
485
486
487
488
489
490
491
492
493 N perform(N n);
494 }
495
496
497
498
499
500
501
502 @Test
503 <M extends GenericIAction<Number>> void test7_fakeImplementationsOfGenericInterface() {
504 GenericIAction<Number> actionNumber = new GenericIAction<Number>() {
505 @Override
506 public Number perform(Number n) {
507 return n.intValue() + 1;
508 }
509 };
510
511 GenericIAction<Integer> actionInt = new GenericIAction<Integer>() {
512 @Override
513 public Integer perform(Integer n) {
514 return n + 1;
515 }
516 };
517
518 GenericIAction<Long> actionL = new GenericIAction<Long>() {
519 @Override
520 public Long perform(Long n) {
521 return n + 2;
522 }
523 };
524
525 new MockUp<M>() {
526 @Mock
527 Number perform(Number n) {
528 return n.intValue() - 1;
529 }
530 };
531
532 Number n = actionNumber.perform(1);
533 assertEquals(0, n);
534
535 int i = actionInt.perform(2);
536 assertEquals(3, i);
537
538 long l = actionL.perform(3L);
539 assertEquals(5, l);
540 }
541
542
543
544
545
546
547
548
549
550
551 @Test
552 <R extends Readable> void test8_excludeJREClassesFromFakingForSafety() throws Exception {
553 new MockUp<R>() {
554 @Mock
555 int read(CharBuffer cb) {
556 return 123;
557 }
558 };
559
560 CharBuffer buf = CharBuffer.allocate(10);
561 int r1 = new Readable() {
562 @Override
563 public int read(@NonNull CharBuffer cb) {
564 return 1;
565 }
566 }.read(buf);
567 assertEquals(123, r1);
568
569 int r2 = new StringReader("test").read(buf);
570 assertEquals(4, r2);
571 }
572 }