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
16
17 final class DelegateInvocationTest {
18
19
20
21
22 static class Collaborator {
23
24
25
26
27 Collaborator() {
28 }
29
30
31
32
33
34
35
36 Collaborator(@SuppressWarnings("unused") int i) {
37 }
38
39
40
41
42
43
44 int getValue() {
45 return -1;
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60 String doSomething(boolean b, int[] i, String s) {
61 return s + b + i[0];
62 }
63
64
65
66
67
68
69 static boolean staticMethod() {
70 return true;
71 }
72
73
74
75
76
77
78
79
80
81 static boolean staticMethod(int i) {
82 return i > 0;
83 }
84
85
86
87
88
89
90
91
92
93 public long publicMethod(boolean b) {
94 return b ? 0L : -1L;
95 }
96 }
97
98
99
100
101
102
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
127
128 static class ConstructorDelegate implements Delegate<Void> {
129
130
131 int capturedArgument;
132
133
134
135
136
137
138
139
140
141 @Mock
142 void init(Invocation context, int i) {
143 assertNotNull(context.getInvokedInstance());
144 capturedArgument = i + context.getInvocationCount();
145 }
146 }
147
148
149
150
151
152
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
172
173
174
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
205
206
207
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
234
235
236
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
262
263
264
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
285
286
287
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
311
312
313
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
349 }
350 }
351
352
353
354
355
356
357
358
359
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
382
383
384
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 }