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
14
15 final class FakeInvocationTest {
16
17
18
19
20 public static class Collaborator {
21
22
23 int value;
24
25
26
27
28 Collaborator() {
29 }
30
31
32
33
34
35
36
37 public Collaborator(int i) {
38 value = i;
39 }
40
41
42
43
44
45
46 public int getValue() {
47 return -1;
48 }
49
50
51
52
53
54
55
56 public void setValue(int i) {
57 value = i;
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public String doSomething(boolean b, int[] i, String s) {
73 return s + b + i[0];
74 }
75
76
77
78
79
80
81 public static boolean staticMethod() {
82 return true;
83 }
84 }
85
86
87
88
89 static final class FakeMethods extends MockUp<Collaborator> {
90
91
92
93
94
95
96
97
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
109
110
111
112
113
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
125
126 @Test
127 void fakeMethodsForMethodsWithoutParameters() {
128 new FakeMethods();
129 assertFalse(Collaborator.staticMethod());
130 assertEquals(123, new Collaborator().getValue());
131 }
132
133
134
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
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
184
185 static class FakeMethodsWithParameters extends MockUp<Collaborator> {
186
187
188 int capturedArgument;
189
190
191 Collaborator fakedInstance;
192
193
194
195
196
197
198
199
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
211
212
213
214
215
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
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
242
243
244
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
264
265 public static final class FakeByMethodNameOnly extends MockUp<Collaborator> {
266
267
268
269
270
271
272
273
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
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
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
316
317 public static final class PublicFakeForConstructorUsingInvocation extends MockUp<Collaborator> {
318
319
320
321
322
323
324
325 @Mock
326 public void $init(Invocation inv) {
327 }
328 }
329
330
331
332
333 @Test
334 void fakeConstructorUsingPublicFakeClassWithPublicFakeMethodHavingInvocationParameter() {
335 new PublicFakeForConstructorUsingInvocation();
336
337 new Collaborator();
338 }
339 }