View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertFalse;
5   import static org.junit.jupiter.api.Assertions.assertNotNull;
6   import static org.junit.jupiter.api.Assertions.assertNull;
7   import static org.junit.jupiter.api.Assertions.assertSame;
8   import static org.junit.jupiter.api.Assertions.assertTrue;
9   
10  import org.junit.jupiter.api.Test;
11  
12  /**
13   * The Class FakeInvocationTest.
14   */
15  final class FakeInvocationTest {
16  
17      /**
18       * The Class Collaborator.
19       */
20      public static class Collaborator {
21  
22          /** The value. */
23          int value;
24  
25          /**
26           * Instantiates a new collaborator.
27           */
28          Collaborator() {
29          }
30  
31          /**
32           * Instantiates a new collaborator.
33           *
34           * @param i
35           *            the i
36           */
37          public Collaborator(int i) {
38              value = i;
39          }
40  
41          /**
42           * Gets the value.
43           *
44           * @return the value
45           */
46          public int getValue() {
47              return -1;
48          }
49  
50          /**
51           * Sets the value.
52           *
53           * @param i
54           *            the new value
55           */
56          public void setValue(int i) {
57              value = i;
58          }
59  
60          /**
61           * Do something.
62           *
63           * @param b
64           *            the b
65           * @param i
66           *            the i
67           * @param s
68           *            the s
69           *
70           * @return the string
71           */
72          public String doSomething(boolean b, int[] i, String s) {
73              return s + b + i[0];
74          }
75  
76          /**
77           * Static method.
78           *
79           * @return true, if successful
80           */
81          public static boolean staticMethod() {
82              return true;
83          }
84      }
85  
86      /**
87       * The Class FakeMethods.
88       */
89      static final class FakeMethods extends MockUp<Collaborator> {
90  
91          /**
92           * Static method.
93           *
94           * @param context
95           *            the context
96           *
97           * @return true, if successful
98           */
99          @Mock
100         static boolean staticMethod(Invocation context) {
101             assertNotNull(context);
102             assertNull(context.getInvokedInstance());
103             assertEquals(1, context.getInvocationCount());
104             return false;
105         }
106 
107         /**
108          * Gets the value.
109          *
110          * @param context
111          *            the context
112          *
113          * @return the value
114          */
115         @Mock
116         int getValue(Invocation context) {
117             assertTrue(context.getInvokedInstance() instanceof Collaborator);
118             assertEquals(0, context.getInvocationIndex());
119             return 123;
120         }
121     }
122 
123     /**
124      * Fake methods for methods without parameters.
125      */
126     @Test
127     void fakeMethodsForMethodsWithoutParameters() {
128         new FakeMethods();
129         assertFalse(Collaborator.staticMethod());
130         assertEquals(123, new Collaborator().getValue());
131     }
132 
133     /**
134      * Instance fake method for static method.
135      */
136     @Test
137     void instanceFakeMethodForStaticMethod() {
138         new MockUp<Collaborator>() {
139             @Mock
140             boolean staticMethod(Invocation context) {
141                 assertNull(context.getInvokedInstance());
142                 assertEquals(context.getInvocationCount() - 1, context.getInvocationIndex());
143                 return context.getInvocationCount() <= 0;
144             }
145         };
146 
147         assertFalse(Collaborator.staticMethod());
148         assertFalse(Collaborator.staticMethod());
149     }
150 
151     /**
152      * Fake methods with invocation parameter.
153      */
154     @Test
155     void fakeMethodsWithInvocationParameter() {
156         new MockUp<Collaborator>() {
157             Collaborator instantiated;
158 
159             @Mock
160             void $init(Invocation inv, int i) {
161                 assertNotNull(inv.getInvokedInstance());
162                 assertTrue(i > 0);
163                 instantiated = inv.getInvokedInstance();
164             }
165 
166             @Mock
167             String doSomething(Invocation inv, boolean b, int[] array, String s) {
168                 assertNotNull(inv);
169                 assertSame(instantiated, inv.getInvokedInstance());
170                 assertEquals(1, inv.getInvocationCount());
171                 assertTrue(b);
172                 assertNull(array);
173                 assertEquals("test", s);
174                 return "mock";
175             }
176         };
177 
178         String s = new Collaborator(123).doSomething(true, null, "test");
179         assertEquals("mock", s);
180     }
181 
182     /**
183      * The Class FakeMethodsWithParameters.
184      */
185     static class FakeMethodsWithParameters extends MockUp<Collaborator> {
186 
187         /** The captured argument. */
188         int capturedArgument;
189 
190         /** The faked instance. */
191         Collaborator fakedInstance;
192 
193         /**
194          * $init.
195          *
196          * @param context
197          *            the context
198          * @param i
199          *            the i
200          */
201         @Mock
202         void $init(Invocation context, int i) {
203             capturedArgument = i + context.getInvocationCount();
204             assertNull(fakedInstance);
205             assertTrue(context.getInvokedInstance() instanceof Collaborator);
206             assertEquals(1, context.getInvokedArguments().length);
207         }
208 
209         /**
210          * Sets the value.
211          *
212          * @param context
213          *            the context
214          * @param i
215          *            the i
216          */
217         @Mock
218         void setValue(Invocation context, int i) {
219             assertEquals(i, context.getInvocationIndex());
220             assertSame(fakedInstance, context.getInvokedInstance());
221             assertEquals(1, context.getInvokedArguments().length);
222         }
223     }
224 
225     /**
226      * Fake methods with parameters.
227      */
228     @Test
229     void fakeMethodsWithParameters() {
230         FakeMethodsWithParameters mock = new FakeMethodsWithParameters();
231 
232         Collaborator col = new Collaborator(4);
233         mock.fakedInstance = col;
234 
235         assertEquals(5, mock.capturedArgument);
236         col.setValue(0);
237         col.setValue(1);
238     }
239 
240     /**
241      * Use of context parameters for JRE methods.
242      *
243      * @throws Exception
244      *             the exception
245      */
246     @Test
247     void useOfContextParametersForJREMethods() throws Exception {
248         new MockUp<Runtime>() {
249             @Mock
250             Process exec(Invocation inv, String command, String[] envp) {
251                 assertSame(Runtime.getRuntime(), inv.getInvokedInstance());
252                 assertEquals(0, inv.getInvocationIndex());
253                 assertNotNull(command);
254                 assertNull(envp);
255                 return null;
256             }
257         };
258 
259         assertNull(Runtime.getRuntime().exec("test", null));
260     }
261 
262     /**
263      * The Class FakeByMethodNameOnly.
264      */
265     public static final class FakeByMethodNameOnly extends MockUp<Collaborator> {
266 
267         /**
268          * Do something.
269          *
270          * @param inv
271          *            the inv
272          *
273          * @return the string
274          */
275         @Mock
276         public static String doSomething(Invocation inv) {
277             Object[] args = inv.getInvokedArguments();
278             assertEquals(3, args.length);
279             return "fake";
280         }
281     }
282 
283     /**
284      * Fake method by name only using public fake.
285      */
286     @Test
287     void fakeMethodByNameOnly_usingPublicFake() {
288         new FakeByMethodNameOnly();
289 
290         String result = new Collaborator().doSomething(true, new int[] { 1, 2 }, "test");
291 
292         assertEquals("fake", result);
293     }
294 
295     /**
296      * Fake method by name only using anonymous fake.
297      */
298     @Test
299     void fakeMethodByNameOnly_usingAnonymousFake() {
300         new MockUp<Collaborator>() {
301             @Mock
302             String doSomething(Invocation inv) {
303                 Object[] args = inv.getInvokedArguments();
304                 assertEquals(3, args.length);
305                 return "fake";
306             }
307         };
308 
309         String result = new Collaborator().doSomething(true, new int[] { 1, 2 }, "test");
310 
311         assertEquals("fake", result);
312     }
313 
314     /**
315      * The Class PublicFakeForConstructorUsingInvocation.
316      */
317     public static final class PublicFakeForConstructorUsingInvocation extends MockUp<Collaborator> {
318 
319         /**
320          * $init.
321          *
322          * @param inv
323          *            the inv
324          */
325         @Mock
326         public void $init(Invocation inv) {
327         }
328     }
329 
330     /**
331      * Fake constructor using public fake class with public fake method having invocation parameter.
332      */
333     @Test
334     void fakeConstructorUsingPublicFakeClassWithPublicFakeMethodHavingInvocationParameter() {
335         new PublicFakeForConstructorUsingInvocation();
336 
337         new Collaborator();
338     }
339 }