1
2
3
4
5
6 package mockit;
7
8 import java.util.Date;
9 import java.util.List;
10
11 import mockit.integration.junit5.JMockitExtension;
12 import mockit.internal.expectations.invocation.MissingInvocation;
13
14 import org.junit.jupiter.api.Assertions;
15 import org.junit.jupiter.api.Test;
16 import org.junit.jupiter.api.extension.ExtendWith;
17
18
19
20
21 @ExtendWith(JMockitExtension.class)
22 class VerificationsWithSomeArgumentMatchersTest {
23
24
25
26
27 @SuppressWarnings("unused")
28 static class Collaborator {
29
30
31
32
33
34
35
36 void setValue(int value) {
37 }
38
39
40
41
42
43
44
45 void setValue(double value) {
46 }
47
48
49
50
51
52
53
54 void setValue(float value) {
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68
69 void setValues(long value1, Byte value2, double value3, Short value4) {
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 boolean booleanValues(long value1, byte value2, double value3, short value4) {
87 return true;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 static void staticSetValues(long value1, byte value2, double value3, short value4) {
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 static long staticLongValues(long value1, byte value2, double value3, char value4) {
120 return -2;
121 }
122
123
124
125
126
127
128
129
130
131
132
133 final void simpleOperation(int a, String b, Date c) {
134 }
135
136
137
138
139
140
141
142
143
144
145
146 long anotherOperation(byte b, long l) {
147 return -1;
148 }
149
150
151
152
153
154
155
156
157
158
159
160 static void staticVoidMethod(long l, char c, float f) {
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175 static boolean staticBooleanMethod(boolean b, String s, int[] array) {
176 return false;
177 }
178 }
179
180
181 @Mocked
182 Collaborator mock;
183
184
185
186
187 @Test
188 void useMatcherOnlyForOneArgument() {
189 mock.simpleOperation(1, "", null);
190 mock.simpleOperation(2, "str", null);
191 mock.simpleOperation(1, "", null);
192 mock.simpleOperation(12, "arg", new Date());
193
194 mock.anotherOperation((byte) 0, 5);
195 mock.anotherOperation((byte) 3, 5);
196
197 Collaborator.staticVoidMethod(34L, '8', 5.0F);
198 Collaborator.staticBooleanMethod(true, "start-end", null);
199
200 new VerificationsInOrder() {
201 {
202 mock.simpleOperation(withEqual(1), "", null);
203 mock.simpleOperation(withNotEqual(1), null, (Date) withNull());
204 mock.simpleOperation(1, withNotEqual("arg"), null);
205 mock.simpleOperation(12, "arg", (Date) withNotNull());
206
207 mock.anotherOperation((byte) 0, anyLong);
208 mock.anotherOperation(anyByte, 5);
209
210 Collaborator.staticVoidMethod(34L, anyChar, 5.0F);
211 Collaborator.staticBooleanMethod(true, withSuffix("end"), null);
212 }
213 };
214 }
215
216
217
218
219 @Test
220 void useMatcherOnlyForFirstArgumentWithUnexpectedReplayValue() {
221 Assertions.assertThrows(MissingInvocation.class, () -> {
222
223 mock.simpleOperation(2, "", null);
224
225 new Verifications() {
226 {
227 mock.simpleOperation(withEqual(1), "", null);
228 }
229 };
230 });
231 }
232
233
234
235
236 @Test
237 void useMatcherOnlyForSecondArgumentWithUnexpectedReplayValue() {
238 Assertions.assertThrows(MissingInvocation.class, () -> {
239
240 mock.simpleOperation(1, "Xyz", null);
241
242 new Verifications() {
243 {
244 mock.simpleOperation(1, withPrefix("arg"), null);
245 }
246 };
247 });
248 }
249
250
251
252
253 @Test
254 void useMatcherOnlyForLastArgumentWithUnexpectedReplayValue() {
255 Assertions.assertThrows(MissingInvocation.class, () -> {
256
257 mock.simpleOperation(12, "arg", null);
258
259 new Verifications() {
260 {
261 mock.simpleOperation(12, "arg", (Date) withNotNull());
262 }
263 };
264 });
265 }
266
267
268
269
270 @Test
271 void useMatchersForParametersOfAllSizes() {
272 mock.setValues(123L, (byte) 5, 6.4, (short) 41);
273 mock.booleanValues(12L, (byte) 4, 6.1, (short) 14);
274 Collaborator.staticSetValues(2L, (byte) 4, 6.1, (short) 3);
275 Collaborator.staticLongValues(12L, (byte) -7, 6.1, 'F');
276
277 new Verifications() {
278 {
279 mock.setValues(123L, anyByte, 6.4, anyShort);
280 mock.booleanValues(12L, (byte) 4, withEqual(6.0, 0.1), withEqual((short) 14));
281 Collaborator.staticSetValues(withNotEqual(1L), (byte) 4, 6.1, withEqual((short) 3));
282 Collaborator.staticLongValues(12L, anyByte, withEqual(6.1), 'F');
283 }
284 };
285 }
286
287
288
289
290 @Test
291 void useAnyIntField() {
292 mock.setValue(1);
293
294 new FullVerifications() {
295 {
296 mock.setValue(anyInt);
297 }
298 };
299 }
300
301
302
303
304 @Test
305 void useSeveralAnyFields() {
306 final Date now = new Date();
307 mock.simpleOperation(2, "abc", now);
308 mock.simpleOperation(5, "test", null);
309 mock.simpleOperation(3, "test2", null);
310 mock.simpleOperation(-1, "Xyz", now);
311 mock.simpleOperation(1, "", now);
312
313 Collaborator.staticSetValues(2, (byte) 1, 0, (short) 2);
314 Collaborator.staticLongValues(23L, (byte) 1, 1.34, 'S');
315 Collaborator.staticVoidMethod(45L, 'S', 56.4F);
316
317 new FullVerifications() {
318 {
319 mock.simpleOperation(anyInt, null, null);
320 mock.simpleOperation(anyInt, "test", null);
321 mock.simpleOperation(3, "test2", null);
322 mock.simpleOperation(-1, null, (Date) any);
323 mock.simpleOperation(1, anyString, now);
324
325 Collaborator.staticSetValues(2L, anyByte, 0.0, anyShort);
326 Collaborator.staticLongValues(anyLong, (byte) 1, anyDouble, anyChar);
327 Collaborator.staticVoidMethod(45L, 'S', anyFloat);
328 }
329 };
330 }
331
332
333
334
335 @Test
336 void useWithMethodsMixedWithAnyFields() {
337 Date now = new Date();
338 mock.simpleOperation(2, "abc", now);
339 mock.simpleOperation(5, "test", null);
340 mock.simpleOperation(3, "test2", null);
341 mock.simpleOperation(-1, "Xyz", now);
342 mock.simpleOperation(1, "", now);
343
344 new Verifications() {
345 {
346 mock.simpleOperation(anyInt, null, (Date) any);
347 mock.simpleOperation(anyInt, withEqual("test"), null);
348 mock.simpleOperation(3, withPrefix("test"), (Date) any);
349 mock.simpleOperation(-1, anyString, (Date) any);
350 mock.simpleOperation(1, anyString, (Date) withNotNull());
351 }
352 };
353 }
354
355
356
357
358 public interface Scheduler {
359
360
361
362
363
364
365
366
367
368
369
370
371 List<String> getAlerts(Object o, int i, boolean b);
372 }
373
374
375
376
377
378
379
380 @Test
381 void useMatchersInInvocationsToInterfaceMethods(@Mocked final Scheduler scheduler) {
382 scheduler.getAlerts("123", 1, true);
383 scheduler.getAlerts(null, 1, false);
384
385 new FullVerifications() {
386 {
387 scheduler.getAlerts(any, 1, anyBoolean);
388 times = 2;
389 }
390 };
391
392 new Verifications() {
393 {
394 scheduler.getAlerts(null, anyInt, anyBoolean);
395 times = 2;
396 }
397 };
398 }
399 }