1
2
3
4
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
19
20 final class FakeInvocationTest {
21
22
23
24
25 public static class Collaborator {
26
27
28 int value;
29
30
31
32
33 Collaborator() {
34 }
35
36
37
38
39
40
41
42 public Collaborator(int i) {
43 value = i;
44 }
45
46
47
48
49
50
51 public int getValue() {
52 return -1;
53 }
54
55
56
57
58
59
60
61 public void setValue(int i) {
62 value = i;
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public String doSomething(boolean b, int[] i, String s) {
78 return s + b + i[0];
79 }
80
81
82
83
84
85
86 public static boolean staticMethod() {
87 return true;
88 }
89 }
90
91
92
93
94 static final class FakeMethods extends MockUp<Collaborator> {
95
96
97
98
99
100
101
102
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
114
115
116
117
118
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
130
131 @Test
132 void fakeMethodsForMethodsWithoutParameters() {
133 new FakeMethods();
134 assertFalse(Collaborator.staticMethod());
135 assertEquals(123, new Collaborator().getValue());
136 }
137
138
139
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
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
189
190 static class FakeMethodsWithParameters extends MockUp<Collaborator> {
191
192
193 int capturedArgument;
194
195
196 Collaborator fakedInstance;
197
198
199
200
201
202
203
204
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
216
217
218
219
220
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
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
247
248
249
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
269
270 public static final class FakeByMethodNameOnly extends MockUp<Collaborator> {
271
272
273
274
275
276
277
278
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
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
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
321
322 public static final class PublicFakeForConstructorUsingInvocation extends MockUp<Collaborator> {
323
324
325
326
327
328
329
330 @Mock
331 public void $init(Invocation inv) {
332 }
333 }
334
335
336
337
338 @Test
339 void fakeConstructorUsingPublicFakeClassWithPublicFakeMethodHavingInvocationParameter() {
340 new PublicFakeForConstructorUsingInvocation();
341
342 new Collaborator();
343 }
344 }