View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import java.applet.Applet;
6   import java.awt.Component;
7   
8   import org.junit.jupiter.api.Test;
9   
10  /**
11   * The Class MisusedFakingAPITest.
12   */
13  final class MisusedFakingAPITest {
14  
15      /**
16       * Fake same method twice with reentrant fakes from two different fake classes.
17       */
18      @Test
19      void fakeSameMethodTwiceWithReentrantFakesFromTwoDifferentFakeClasses() {
20          new MockUp<Applet>() {
21              @Mock
22              int getComponentCount(Invocation inv) {
23                  int i = inv.proceed();
24                  return i + 1;
25              }
26          };
27  
28          int i = new Applet().getComponentCount();
29          assertEquals(1, i);
30  
31          new MockUp<Applet>() {
32              @Mock
33              int getComponentCount(Invocation inv) {
34                  int j = inv.proceed();
35                  return j + 2;
36              }
37          };
38  
39          // Should return 3, but returns 5. Chaining mock methods is not supported.
40          int j = new Applet().getComponentCount();
41          assertEquals(5, j);
42      }
43  
44      /**
45       * The Class AppletFake.
46       */
47      static final class AppletFake extends MockUp<Applet> {
48  
49          /** The component count. */
50          final int componentCount;
51  
52          /**
53           * Instantiates a new applet fake.
54           *
55           * @param componentCount
56           *            the component count
57           */
58          AppletFake(int componentCount) {
59              this.componentCount = componentCount;
60          }
61  
62          /**
63           * Gets the component count.
64           *
65           * @param inv
66           *            the inv
67           *
68           * @return the component count
69           */
70          @Mock
71          int getComponentCount(Invocation inv) {
72              return componentCount;
73          }
74      }
75  
76      /**
77       * Apply the same fake for A class twice.
78       */
79      @Test
80      void applyTheSameFakeForAClassTwice() {
81          new AppletFake(1);
82          new AppletFake(2); // second application overrides the previous one
83  
84          assertEquals(2, new Applet().getComponentCount());
85      }
86  
87      /**
88       * Fake A private method.
89       */
90      @Test
91      void fakeAPrivateMethod() {
92          // Changed to allow fake private constructors.
93          new MockUp<Component>() {
94              @Mock
95              boolean checkCoalescing() {
96                  return false;
97              }
98          };
99      }
100 
101     /**
102      * Fake A private constructor.
103      */
104     @Test
105     public void fakeAPrivateConstructor() {
106         // Changed to allow fake private constructors.
107         new MockUp<System>() {
108             @Mock
109             void $init() {
110             }
111         };
112     }
113 }