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