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