View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
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   * The Class VerificationsWithSomeArgumentMatchersTest.
20   */
21  @ExtendWith(JMockitExtension.class)
22  class VerificationsWithSomeArgumentMatchersTest {
23  
24      /**
25       * The Class Collaborator.
26       */
27      @SuppressWarnings("unused")
28      static class Collaborator {
29  
30          /**
31           * Sets the value.
32           *
33           * @param value
34           *            the new value
35           */
36          void setValue(int value) {
37          }
38  
39          /**
40           * Sets the value.
41           *
42           * @param value
43           *            the new value
44           */
45          void setValue(double value) {
46          }
47  
48          /**
49           * Sets the value.
50           *
51           * @param value
52           *            the new value
53           */
54          void setValue(float value) {
55          }
56  
57          /**
58           * Sets the values.
59           *
60           * @param value1
61           *            the value 1
62           * @param value2
63           *            the value 2
64           * @param value3
65           *            the value 3
66           * @param value4
67           *            the value 4
68           */
69          void setValues(long value1, Byte value2, double value3, Short value4) {
70          }
71  
72          /**
73           * Boolean values.
74           *
75           * @param value1
76           *            the value 1
77           * @param value2
78           *            the value 2
79           * @param value3
80           *            the value 3
81           * @param value4
82           *            the value 4
83           *
84           * @return true, if successful
85           */
86          boolean booleanValues(long value1, byte value2, double value3, short value4) {
87              return true;
88          }
89  
90          /**
91           * Static set values.
92           *
93           * @param value1
94           *            the value 1
95           * @param value2
96           *            the value 2
97           * @param value3
98           *            the value 3
99           * @param value4
100          *            the value 4
101          */
102         static void staticSetValues(long value1, byte value2, double value3, short value4) {
103         }
104 
105         /**
106          * Static long values.
107          *
108          * @param value1
109          *            the value 1
110          * @param value2
111          *            the value 2
112          * @param value3
113          *            the value 3
114          * @param value4
115          *            the value 4
116          *
117          * @return the long
118          */
119         static long staticLongValues(long value1, byte value2, double value3, char value4) {
120             return -2;
121         }
122 
123         /**
124          * Simple operation.
125          *
126          * @param a
127          *            the a
128          * @param b
129          *            the b
130          * @param c
131          *            the c
132          */
133         final void simpleOperation(int a, String b, Date c) {
134         }
135 
136         /**
137          * Another operation.
138          *
139          * @param b
140          *            the b
141          * @param l
142          *            the l
143          *
144          * @return the long
145          */
146         long anotherOperation(byte b, long l) {
147             return -1;
148         }
149 
150         /**
151          * Static void method.
152          *
153          * @param l
154          *            the l
155          * @param c
156          *            the c
157          * @param f
158          *            the f
159          */
160         static void staticVoidMethod(long l, char c, float f) {
161         }
162 
163         /**
164          * Static boolean method.
165          *
166          * @param b
167          *            the b
168          * @param s
169          *            the s
170          * @param array
171          *            the array
172          *
173          * @return true, if successful
174          */
175         static boolean staticBooleanMethod(boolean b, String s, int[] array) {
176             return false;
177         }
178     }
179 
180     /** The mock. */
181     @Mocked
182     Collaborator mock;
183 
184     /**
185      * Use matcher only for one argument.
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      * Use matcher only for first argument with unexpected replay value.
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      * Use matcher only for second argument with unexpected replay value.
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      * Use matcher only for last argument with unexpected replay value.
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      * Use matchers for parameters of all sizes.
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      * Use any int field.
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      * Use several any fields.
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      * Use with methods mixed with any fields.
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      * The Interface Scheduler.
357      */
358     public interface Scheduler {
359         /**
360          * Gets the alerts.
361          *
362          * @param o
363          *            the o
364          * @param i
365          *            the i
366          * @param b
367          *            the b
368          *
369          * @return the alerts
370          */
371         List<String> getAlerts(Object o, int i, boolean b);
372     }
373 
374     /**
375      * Use matchers in invocations to interface methods.
376      *
377      * @param scheduler
378      *            the scheduler
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 }