View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNotNull;
5   import static org.junit.jupiter.api.Assertions.assertNull;
6   import static org.junit.jupiter.api.Assertions.assertSame;
7   import static org.junit.jupiter.api.Assertions.assertTrue;
8   import static org.junit.jupiter.api.Assertions.fail;
9   
10  import java.util.concurrent.Callable;
11  
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * The Class DelegateInvocationTest.
16   */
17  final class DelegateInvocationTest {
18  
19      /**
20       * The Class Collaborator.
21       */
22      static class Collaborator {
23  
24          /**
25           * Instantiates a new collaborator.
26           */
27          Collaborator() {
28          }
29  
30          /**
31           * Instantiates a new collaborator.
32           *
33           * @param i
34           *            the i
35           */
36          Collaborator(@SuppressWarnings("unused") int i) {
37          }
38  
39          /**
40           * Gets the value.
41           *
42           * @return the value
43           */
44          int getValue() {
45              return -1;
46          }
47  
48          /**
49           * Do something.
50           *
51           * @param b
52           *            the b
53           * @param i
54           *            the i
55           * @param s
56           *            the s
57           *
58           * @return the string
59           */
60          String doSomething(boolean b, int[] i, String s) {
61              return s + b + i[0];
62          }
63  
64          /**
65           * Static method.
66           *
67           * @return true, if successful
68           */
69          static boolean staticMethod() {
70              return true;
71          }
72  
73          /**
74           * Static method.
75           *
76           * @param i
77           *            the i
78           *
79           * @return true, if successful
80           */
81          static boolean staticMethod(int i) {
82              return i > 0;
83          }
84  
85          /**
86           * Public method.
87           *
88           * @param b
89           *            the b
90           *
91           * @return the long
92           */
93          public long publicMethod(boolean b) {
94              return b ? 0L : -1L;
95          }
96      }
97  
98      /**
99       * Delegate with context object.
100      *
101      * @param unused
102      *            the unused
103      */
104     @Test
105     void delegateWithContextObject(@Mocked Collaborator unused) {
106         new Expectations() {
107             {
108                 Collaborator.staticMethod();
109                 result = new Delegate<Object>() {
110                     @Mock
111                     boolean staticMethod(Invocation context) {
112                         assertNull(context.getInvokedInstance());
113                         assertEquals(0, context.getInvokedArguments().length);
114                         assertEquals(context.getInvocationCount() - 1, context.getInvocationIndex());
115                         return context.getInvocationCount() > 0;
116                     }
117                 };
118             }
119         };
120 
121         assertTrue(Collaborator.staticMethod());
122         assertTrue(Collaborator.staticMethod());
123     }
124 
125     /**
126      * The Class ConstructorDelegate.
127      */
128     static class ConstructorDelegate implements Delegate<Void> {
129 
130         /** The captured argument. */
131         int capturedArgument;
132 
133         /**
134          * Inits the.
135          *
136          * @param context
137          *            the context
138          * @param i
139          *            the i
140          */
141         @Mock
142         void init(Invocation context, int i) {
143             assertNotNull(context.getInvokedInstance());
144             capturedArgument = i + context.getInvocationCount();
145         }
146     }
147 
148     /**
149      * Delegate for constructor with context.
150      *
151      * @param mock
152      *            the mock
153      */
154     @Test
155     void delegateForConstructorWithContext(@Mocked Collaborator mock) {
156         final ConstructorDelegate delegate = new ConstructorDelegate();
157 
158         new Expectations() {
159             {
160                 new Collaborator(anyInt);
161                 result = delegate;
162             }
163         };
164 
165         new Collaborator(4);
166 
167         assertEquals(5, delegate.capturedArgument);
168     }
169 
170     /**
171      * Delegate receiving null arguments.
172      *
173      * @param mock
174      *            the mock
175      */
176     @Test
177     void delegateReceivingNullArguments(@Mocked final Collaborator mock) {
178         new Expectations() {
179             {
180                 mock.doSomething(true, null, null);
181                 result = new Delegate<Object>() {
182                     @Mock
183                     void doSomething(Invocation invocation, Boolean b, int[] i, String s) {
184                         Collaborator instance = invocation.getInvokedInstance();
185                         assertSame(mock, instance);
186                         assertEquals(1, invocation.getInvocationCount());
187                         assertTrue(b);
188                         assertNull(i);
189                         assertNull(s);
190                         Object[] args = invocation.getInvokedArguments();
191                         assertEquals(3, args.length);
192                         assertTrue((Boolean) args[0]);
193                         assertNull(args[1]);
194                         assertNull(args[2]);
195                     }
196                 };
197             }
198         };
199 
200         assertNull(mock.doSomething(true, null, null));
201     }
202 
203     /**
204      * Delegate with another method on the delegate class.
205      *
206      * @param mock
207      *            the mock
208      */
209     @Test
210     void delegateWithAnotherMethodOnTheDelegateClass(@Mocked final Collaborator mock) {
211         new Expectations() {
212             {
213                 mock.getValue();
214                 result = new Delegate<Object>() {
215                     @Mock
216                     int getValue(Invocation context) {
217                         return context.getInvocationCount();
218                     }
219 
220                     @SuppressWarnings("unused")
221                     private void otherMethod(Invocation context) {
222                         fail();
223                     }
224                 };
225             }
226         };
227 
228         assertEquals(1, new Collaborator().getValue());
229         assertEquals(2, new Collaborator().getValue());
230     }
231 
232     /**
233      * Delegate class with multiple methods and inexact but valid match.
234      *
235      * @param mock
236      *            the mock
237      */
238     @Test
239     void delegateClassWithMultipleMethodsAndInexactButValidMatch(@Mocked Collaborator mock) {
240         new Expectations() {
241             {
242                 Collaborator.staticMethod(1);
243                 result = new Delegate<Object>() {
244                     @SuppressWarnings("unused")
245                     private void otherMethod(int i) {
246                         fail();
247                     }
248 
249                     @Mock
250                     boolean staticMethod(Invocation invocation, Number i) {
251                         return i.intValue() > 0;
252                     }
253                 };
254             }
255         };
256 
257         assertTrue(Collaborator.staticMethod(1));
258     }
259 
260     /**
261      * Delegate method with no parameters for expectation with parameters.
262      *
263      * @param mock
264      *            the mock
265      */
266     @Test
267     void delegateMethodWithNoParametersForExpectationWithParameters(@Mocked final Collaborator mock) {
268         new Expectations() {
269             {
270                 mock.publicMethod(true);
271                 result = new Delegate<Object>() {
272                     @Mock
273                     long nonMatchingDelegate() {
274                         return 123L;
275                     }
276                 };
277             }
278         };
279 
280         assertEquals(123, mock.publicMethod(true));
281     }
282 
283     /**
284      * Delegate with different method name.
285      *
286      * @param mock
287      *            the mock
288      */
289     @Test
290     void delegateWithDifferentMethodName(@Mocked final Collaborator mock) {
291         new Expectations() {
292             {
293                 mock.publicMethod(anyBoolean);
294                 result = new Delegate<Object>() {
295                     @Mock
296                     long differentName(Invocation invocation, boolean b) {
297                         assertEquals(1, invocation.getInvocationCount());
298                         assertTrue(b);
299                         assertSame(Boolean.TRUE, invocation.getInvokedArguments()[0]);
300                         return 3L;
301                     }
302                 };
303             }
304         };
305 
306         assertEquals(3L, new Collaborator().publicMethod(true));
307     }
308 
309     /**
310      * Consecutive delegates for the same expectation.
311      *
312      * @param mock
313      *            the mock
314      */
315     @Test
316     void consecutiveDelegatesForTheSameExpectation(@Mocked final Collaborator mock) {
317         new Expectations() {
318             {
319                 mock.getValue();
320                 returns(new Delegate<Object>() {
321                     @Mock
322                     int delegate(Invocation invocation) {
323                         assertSame(mock, invocation.getInvokedInstance());
324                         return invocation.getInvocationCount();
325                     }
326                 }, new Delegate<Object>() {
327                     @Mock
328                     int delegate(Invocation invocation) {
329                         return invocation.getInvocationCount();
330                     }
331                 }, new Delegate<Object>() {
332                     @Mock
333                     int delegate(Invocation invocation) {
334                         assertEquals(3, invocation.getInvocationCount());
335                         throw new SecurityException();
336                     }
337                 });
338             }
339         };
340 
341         assertEquals(1, mock.getValue());
342         assertEquals(2, mock.getValue());
343 
344         try {
345             mock.getValue();
346             fail();
347         } catch (SecurityException ignore) {
348             // OK
349         }
350     }
351 
352     /**
353      * Delegate method with invocation for interface.
354      *
355      * @param mock
356      *            the mock
357      *
358      * @throws Exception
359      *             the exception
360      */
361     @Test
362     void delegateMethodWithInvocationForInterface(@Mocked final Callable<String> mock) throws Exception {
363         new Expectations() {
364             {
365                 mock.call();
366                 result = new Delegate<Object>() {
367                     @Mock
368                     String delegate(Invocation inv) {
369                         return inv.getInvokedMember().getDeclaringClass().getName();
370                     }
371                 };
372             }
373         };
374 
375         String s = mock.call();
376 
377         assertEquals(Callable.class.getName(), s);
378     }
379 
380     /**
381      * Use of context parameters for JRE methods.
382      *
383      * @throws Exception
384      *             the exception
385      */
386     @Test
387     void useOfContextParametersForJREMethods() throws Exception {
388         final Runtime rt = Runtime.getRuntime();
389 
390         new Expectations(rt) {
391             {
392                 rt.exec(anyString, null);
393                 maxTimes = 1;
394                 result = new Delegate<Object>() {
395                     @Mock
396                     void exec(Invocation inv, String command, String[] envp) {
397                         assertSame(rt, inv.getInvokedInstance());
398                         assertEquals(0, inv.getInvocationIndex());
399                         assertNotNull(command);
400                         assertNull(envp);
401                     }
402                 };
403             }
404         };
405 
406         assertNull(rt.exec("test", null));
407     }
408 }