View Javadoc
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   * The Class FakingBaseTypesTest.
21   */
22  @TestMethodOrder(MethodName.class)
23  final class FakingBaseTypesTest {
24  
25      /**
26       * The Class BeforeClassBaseAction.
27       */
28      static class BeforeClassBaseAction {
29          /**
30           * Perform.
31           *
32           * @return the int
33           */
34          protected int perform() {
35              return -1;
36          }
37      }
38  
39      /**
40       * The Class BeforeClassAction.
41       */
42      static class BeforeClassAction extends BeforeClassBaseAction {
43          @Override
44          protected int perform() {
45              return 12;
46          }
47      }
48  
49      /**
50       * Apply fake for all tests.
51       *
52       * @param <T>
53       *            the generic type
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       * Verify fake for all tests is in effect.
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       * The Interface IBeforeAction.
79       */
80      public interface IBeforeAction {
81          /**
82           * Perform.
83           *
84           * @return the int
85           */
86          @SuppressWarnings("unused")
87          int perform();
88      }
89  
90      /**
91       * The Class BeforeAction.
92       */
93      static class BeforeAction implements IBeforeAction {
94          @Override
95          public int perform() {
96              return 123;
97          }
98      }
99  
100     /**
101      * Apply fake for each test.
102      *
103      * @param <T>
104      *            the generic type
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      * Verify fake for each test is in effect.
118      */
119     @AfterEach
120     void verifyFakeForEachTestIsInEffect() {
121         int i = new BeforeAction().perform();
122         assertEquals(56, i);
123     }
124 
125     /**
126      * The Interface IAction.
127      */
128     public interface IAction {
129 
130         /**
131          * Perform.
132          *
133          * @param i
134          *            the i
135          *
136          * @return the int
137          */
138         int perform(int i);
139 
140         /**
141          * Not to be faked.
142          *
143          * @return true, if successful
144          */
145         boolean notToBeFaked();
146     }
147 
148     /**
149      * The Class ActionImpl1.
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      * The Class ActionImpl2.
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     /** The action I. */
179     IAction actionI;
180 
181     /**
182      * Test 3 fake interface implementation classes using anonymous fake.
183      *
184      * @param <T>
185      *            the generic type
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      * The Interface TestInterface.
208      */
209     public interface TestInterface {
210         /**
211          * Gets the data.
212          *
213          * @return the data
214          */
215         String getData();
216     }
217 
218     /**
219      * The Class FakeTestInterface.
220      *
221      * @param <T>
222      *            the generic type
223      */
224     public static final class FakeTestInterface<T extends TestInterface> extends MockUp<T> {
225 
226         /**
227          * Gets the data.
228          *
229          * @param inv
230          *            the inv
231          *
232          * @return the data
233          */
234         @Mock
235         public String getData(Invocation inv) {
236             return "faked" + inv.proceed();
237         }
238     }
239 
240     /**
241      * Fake all classes implementing an interface using named fake with invocation parameter.
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      * The Class BaseAction.
268      */
269     public abstract static class BaseAction {
270 
271         /**
272          * Perform.
273          *
274          * @param i
275          *            the i
276          *
277          * @return the int
278          */
279         protected abstract int perform(int i);
280 
281         /**
282          * To be faked as well.
283          *
284          * @return the int
285          */
286         public int toBeFakedAsWell() {
287             return -1;
288         }
289 
290         /**
291          * Not to be faked.
292          *
293          * @return the int
294          */
295         int notToBeFaked() {
296             return 1;
297         }
298     }
299 
300     /**
301      * The Class ConcreteAction1.
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      * The Class ConcreteAction2.
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      * The Class ConcreteAction3.
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     /** The action B. */
351     BaseAction actionB;
352 
353     /**
354      * Test 4 fake concrete subclasses using anonymous fake.
355      *
356      * @param <T>
357      *            the generic type
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      * Check implementation classes are no longer faked.
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      * The Class FakeInterface.
411      *
412      * @param <T>
413      *            the generic type
414      */
415     static final class FakeInterface<T extends IAction> extends MockUp<T> {
416 
417         /**
418          * Perform.
419          *
420          * @param i
421          *            the i
422          *
423          * @return the int
424          */
425         @Mock
426         int perform(int i) {
427             return i + 2;
428         }
429     }
430 
431     /**
432      * Test 5 fake interface implementation classes using named fake.
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      * The Class FakeBaseClass.
445      *
446      * @param <T>
447      *            the generic type
448      */
449     static final class FakeBaseClass<T extends BaseAction> extends MockUp<T> {
450 
451         /**
452          * Perform.
453          *
454          * @param i
455          *            the i
456          *
457          * @return the int
458          */
459         @Mock
460         int perform(int i) {
461             return i + 3;
462         }
463     }
464 
465     /**
466      * Test 6 fake concrete subclasses using named fake.
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      * The Interface GenericIAction.
480      *
481      * @param <N>
482      *            the number type
483      */
484     interface GenericIAction<N extends Number> {
485         /**
486          * Perform.
487          *
488          * @param n
489          *            the n
490          *
491          * @return the n
492          */
493         N perform(N n);
494     }
495 
496     /**
497      * Test 7 fake implementations of generic interface.
498      *
499      * @param <M>
500      *            the generic type
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); // mocked
534 
535         int i = actionInt.perform(2);
536         assertEquals(3, i); // not mocked
537 
538         long l = actionL.perform(3L);
539         assertEquals(5, l); // not mocked
540     }
541 
542     /**
543      * Test 8 exclude JRE classes from faking for safety.
544      *
545      * @param <R>
546      *            the generic type
547      *
548      * @throws Exception
549      *             the exception
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 }