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