View Javadoc
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.assertNull;
6   import static org.junit.jupiter.api.Assertions.assertTrue;
7   
8   import org.junit.jupiter.api.Test;
9   
10  /**
11   * The Class ExpectationsWithDuplicateRecordingsTest.
12   */
13  final class ExpectationsWithDuplicateRecordingsTest {
14  
15      /**
16       * The Class Blah.
17       */
18      @SuppressWarnings({ "unused", "NumericCastThatLosesPrecision" })
19      static class Blah {
20  
21          /**
22           * Sets the value.
23           *
24           * @param value
25           *            the new value
26           */
27          void setValue(int value) {
28          }
29  
30          /**
31           * Do something.
32           *
33           * @param b
34           *            the b
35           *
36           * @return the string
37           */
38          String doSomething(boolean b) {
39              return "";
40          }
41  
42          /**
43           * Do something.
44           *
45           * @param s
46           *            the s
47           *
48           * @return the string
49           */
50          String doSomething(String s) {
51              return "";
52          }
53  
54          /**
55           * Do something.
56           *
57           * @param l
58           *            the l
59           *
60           * @return the long
61           */
62          long doSomething(Long l) {
63              return -1L;
64          }
65  
66          /**
67           * Do something.
68           *
69           * @param l
70           *            the l
71           * @param o
72           *            the o
73           *
74           * @return the long
75           */
76          long doSomething(Long l, Object o) {
77              return 1L;
78          }
79  
80          /**
81           * Truncate.
82           *
83           * @param d
84           *            the d
85           *
86           * @return the int
87           */
88          int truncate(double d) {
89              return (int) d;
90          }
91  
92          /**
93           * Truncate.
94           *
95           * @param f
96           *            the f
97           *
98           * @return the int
99           */
100         int truncate(float f) {
101             return (int) f;
102         }
103 
104         /**
105          * Do something.
106          *
107          * @param i
108          *            the i
109          *
110          * @return the boolean
111          */
112         Boolean doSomething(int i) {
113             return true;
114         }
115 
116         /**
117          * Do something.
118          *
119          * @param c
120          *            the c
121          *
122          * @return the int
123          */
124         int doSomething(char c) {
125             return 123;
126         }
127     }
128 
129     /** The mock. */
130     @Mocked
131     Blah mock;
132 
133     /**
134      * Record same method with disjunctive argument matchers.
135      */
136     @Test
137     void recordSameMethodWithDisjunctiveArgumentMatchers() {
138         new Expectations() {
139             {
140                 mock.doSomething(withEqual(1));
141                 result = false;
142                 mock.doSomething(withNotEqual(1));
143                 result = true;
144             }
145         };
146 
147         assertFalse(mock.doSomething(1));
148         assertTrue(mock.doSomething(2));
149         assertTrue(mock.doSomething(0));
150         assertFalse(mock.doSomething(1));
151     }
152 
153     /**
154      * Record ambiguous expectations using argument matchers.
155      */
156     @Test
157     void recordAmbiguousExpectationsUsingArgumentMatchers() {
158         new Expectations() {
159             {
160                 mock.doSomething(withNotEqual('x'));
161                 result = 1;
162                 mock.doSomething(anyChar);
163                 result = 2;
164             }
165         };
166 
167         assertEquals(1, mock.doSomething('W'));
168         assertEquals(2, mock.doSomething('x'));
169     }
170 
171     /**
172      * Record same method with identical argument matchers.
173      */
174     @Test
175     void recordSameMethodWithIdenticalArgumentMatchers() {
176         new Expectations() {
177             {
178                 mock.doSomething(anyInt);
179                 result = false;
180                 mock.doSomething(anyInt);
181                 result = true; // overrides the previous expectation
182 
183                 mock.doSomething(withNotEqual(5L), withInstanceOf(String.class));
184                 result = 1L;
185                 mock.doSomething(withNotEqual(5L), withInstanceOf(String.class));
186                 result = 2L; // same here
187             }
188         };
189 
190         assertTrue(mock.doSomething(1));
191         assertTrue(mock.doSomething(0));
192 
193         assertEquals(2, mock.doSomething(null, "test 1"));
194         assertEquals(2, mock.doSomething(1L, "test 2"));
195     }
196 
197     /**
198      * Record same method with overlapping argument matchers.
199      */
200     @Test
201     void recordSameMethodWithOverlappingArgumentMatchers() {
202         new Expectations() {
203             {
204                 mock.doSomething(withEqual(0));
205                 result = false;
206                 mock.doSomething(anyInt);
207                 result = true;
208 
209                 mock.doSomething((Long) withNull());
210                 result = 1L;
211                 mock.doSomething((Long) any);
212                 result = 2L;
213             }
214         };
215 
216         assertTrue(mock.doSomething(1));
217         assertFalse(mock.doSomething(0));
218 
219         assertEquals(1, mock.doSomething((Long) null));
220         assertEquals(2, mock.doSomething(1L));
221     }
222 
223     /**
224      * Record same method with exact argument and arg matcher but in wrong order of specificity.
225      */
226     @Test
227     void recordSameMethodWithExactArgumentAndArgMatcherButInWrongOrderOfSpecificity() {
228         new Expectations() {
229             {
230                 mock.doSomething(anyInt);
231                 result = false;
232                 mock.doSomething(1);
233                 result = true; // overrides the previous one (most specific should come first)
234             }
235         };
236 
237         assertTrue(mock.doSomething(1)); // matches last recorded expectation
238         assertFalse(mock.doSomething(2)); // matches no expectation
239     }
240 
241     /**
242      * Record same method with arguments or matchers of varying specificity.
243      */
244     @Test
245     void recordSameMethodWithArgumentsOrMatchersOfVaryingSpecificity() {
246         new Expectations() {
247             {
248                 mock.doSomething(true);
249                 result = null;
250                 mock.doSomething(anyBoolean);
251                 result = "a";
252 
253                 mock.doSomething(1);
254                 result = true;
255                 mock.doSomething(anyInt);
256                 result = false;
257 
258                 mock.doSomething(withEqual('c'));
259                 result = 1;
260                 mock.doSomething(anyChar);
261                 result = 2;
262 
263                 mock.doSomething((String) withNull());
264                 mock.doSomething(withEqual("str"));
265                 result = "b";
266                 mock.doSomething(anyString);
267                 result = "c";
268             }
269         };
270 
271         assertEquals("a", mock.doSomething(false)); // matches only one expectation
272         assertNull(mock.doSomething(true)); // matches two, but most specific was recorded first
273 
274         assertTrue(mock.doSomething(1)); // matches two, but most specific came first
275         assertFalse(mock.doSomething(2)); // matches only one expectation
276 
277         assertEquals(1, mock.doSomething('c')); // matches the first and most specific
278         assertEquals(2, mock.doSomething('3')); // matches only one
279         assertEquals(2, mock.doSomething('x')); // matches only one
280 
281         assertNull(mock.doSomething((String) null)); // matches one specific expectation
282         assertEquals("b", mock.doSomething("str")); // matches another specific expectation
283         assertEquals("c", mock.doSomething("")); // matches the non-specific expectation
284     }
285 
286     /**
287      * Record same method with opposing matchers.
288      */
289     @Test
290     void recordSameMethodWithOpposingMatchers() {
291         new Expectations() {
292             {
293                 mock.doSomething(this.<String> withNull());
294                 result = "null";
295                 mock.doSomething(this.<String> withNotNull());
296                 result = "non-null";
297             }
298         };
299 
300         assertEquals("non-null", mock.doSomething("XYZ"));
301         assertEquals("null", mock.doSomething((String) null));
302     }
303 
304     /**
305      * Record ambiguous expectations using the same matcher but with different arguments.
306      */
307     @Test
308     void recordAmbiguousExpectationsUsingTheSameMatcherButWithDifferentArguments() {
309         new Expectations() {
310             {
311                 mock.doSomething(withNotEqual('A'));
312                 result = 1;
313                 mock.doSomething(withNotEqual('B'));
314                 result = 2; // overrides the previous expectation
315 
316                 mock.doSomething(withAny(1));
317                 result = false;
318                 mock.doSomething(withAny(2));
319                 result = true; // overrides the previous expectation
320             }
321         };
322 
323         assertEquals(2, mock.doSomething('A'));
324         assertEquals(0, mock.doSomething('B'));
325         assertEquals(2, mock.doSomething(' '));
326 
327         assertTrue(mock.doSomething(3));
328         assertTrue(mock.doSomething(1));
329         assertTrue(mock.doSomething(2));
330     }
331 
332     /**
333      * Record unambiguous expectations using the same matcher but with different arguments.
334      */
335     @Test
336     @SuppressWarnings("unused")
337     void recordUnambiguousExpectationsUsingTheSameMatcherButWithDifferentArguments() {
338         new Expectations() {
339             {
340                 mock.doSomething(withEqual("abc"));
341                 result = "first";
342                 mock.doSomething(withEqual("XYZ"));
343                 result = "second";
344 
345                 mock.truncate(withEqual(5.0, 0.1));
346                 result = 1;
347                 mock.truncate(withEqual(-5.0, 0.005));
348                 result = 2;
349 
350                 mock.truncate(withEqual(300.0F, 2));
351                 result = 1;
352                 mock.truncate(withEqual(123.5F, 1));
353                 result = 2;
354 
355                 mock.doSomething(withSameInstance('A'));
356                 result = 1;
357                 mock.doSomething(withSameInstance('B'));
358                 result = 2;
359 
360                 mock.doSomething(1L, withInstanceOf(Long.class));
361                 result = 1L;
362                 mock.doSomething(1L, withInstanceOf(Integer.class));
363                 result = 2L;
364 
365                 mock.doSomething(2L, withInstanceLike(123L));
366                 result = 1L;
367                 mock.doSomething(2L, withInstanceLike(123));
368                 result = 2L;
369 
370                 mock.doSomething(withPrefix("Abc"));
371                 result = "Ap";
372                 mock.doSomething(withPrefix("Xyz"));
373                 result = "Bp";
374 
375                 mock.doSomething(withSuffix("S"));
376                 result = "As";
377                 mock.doSomething(withSuffix("suf"));
378                 result = "Bs";
379 
380                 mock.doSomething(withSubstring("abc"));
381                 result = "sub1";
382                 mock.doSomething(withSubstring("5X"));
383                 result = "sub2";
384 
385                 mock.doSomething(withMatch("[A-F]+"));
386                 result = "letter";
387                 mock.doSomething(withMatch("[0-9]+"));
388                 result = "digit";
389 
390                 mock.doSomething(with(new Delegate<Boolean>() {
391                     boolean matches(boolean b) {
392                         return b;
393                     }
394                 }));
395                 result = "T";
396                 mock.doSomething(with(new Delegate<Boolean>() {
397                     boolean matches(boolean b) {
398                         return !b;
399                     }
400                 }));
401                 result = "F";
402 
403                 mock.doSomething(with(new Delegate<Integer>() {
404                     boolean matches(int i) {
405                         return i > 0;
406                     }
407                 }));
408                 result = true;
409                 mock.doSomething(with(new Delegate<Integer>() {
410                     boolean matches(int i) {
411                         return i < 0;
412                     }
413                 }));
414                 result = false;
415 
416                 mock.doSomething(with(new Delegate<Long>() {
417                     boolean matches(Long l) {
418                         return l == null;
419                     }
420                 }));
421                 result = 1L;
422                 mock.doSomething(with(new Delegate<Long>() {
423                     boolean matches(Long l) {
424                         return l != null;
425                     }
426                 }));
427                 result = 2L;
428             }
429         };
430 
431         assertEquals("second", mock.doSomething("XYZ"));
432         assertEquals("first", mock.doSomething("abc"));
433 
434         assertEquals(2, mock.truncate(-5.001));
435         assertEquals(1, mock.truncate(4.92));
436 
437         assertEquals(2, mock.truncate(123.5F));
438         assertEquals(1, mock.truncate(301.9F));
439 
440         assertEquals(0, mock.doSomething(' '));
441         assertEquals(1, mock.doSomething('A'));
442         assertEquals(2, mock.doSomething('B'));
443 
444         assertEquals(2L, mock.doSomething(1L, 123));
445         assertEquals(1L, mock.doSomething(1L, 123L));
446 
447         assertEquals(2L, mock.doSomething(2L, 123));
448         assertEquals(1L, mock.doSomething(2L, 123L));
449 
450         assertEquals("Bp", mock.doSomething("XyzAbc"));
451         assertEquals("Ap", mock.doSomething("AbcXyz"));
452 
453         assertEquals("Bs", mock.doSomething("asDfsuf"));
454         assertEquals("As", mock.doSomething("sfj43S"));
455 
456         assertEquals("sub2", mock.doSomething(" 5X  234 TY"));
457         assertEquals("sub1", mock.doSomething("AsdabcJ343"));
458 
459         assertEquals("digit", mock.doSomething("34502"));
460         assertEquals("letter", mock.doSomething("AEFCB"));
461 
462         assertEquals("T", mock.doSomething(true));
463         assertEquals("F", mock.doSomething(false));
464 
465         assertTrue(mock.doSomething(34));
466         assertFalse(mock.doSomething(0));
467         assertFalse(mock.doSomething(-12));
468 
469         assertEquals(1L, mock.doSomething((Long) null));
470         assertEquals(2L, mock.doSomething(123L));
471     }
472 
473     /**
474      * Record unambiguous expectations with same matcher for one parameter and different arguments for another.
475      */
476     @Test
477     void recordUnambiguousExpectationsWithSameMatcherForOneParameterAndDifferentArgumentsForAnother() {
478         Blah b = new Blah();
479 
480         new Expectations() {
481             {
482                 mock.doSomething(anyLong, "A");
483                 result = 1L;
484                 mock.doSomething(anyLong, "B");
485                 result = 2L;
486             }
487         };
488 
489         assertEquals(1L, b.doSomething(1L, "A"));
490         assertEquals(2L, b.doSomething(2L, "B"));
491         assertEquals(0L, b.doSomething(1L, "c"));
492         assertEquals(1L, b.doSomething(1L, "A"));
493         assertEquals(2L, b.doSomething(0L, "B"));
494         assertEquals(0L, b.doSomething(0L, null));
495     }
496 
497     /**
498      * Record overlapping expectations with same matcher for one parameter and different arguments for another.
499      */
500     @Test
501     void recordOverlappingExpectationsWithSameMatcherForOneParameterAndDifferentArgumentsForAnother() {
502         Blah b = new Blah();
503 
504         new Expectations() {
505             {
506                 mock.doSomething(anyLong, "A");
507                 result = 1L;
508                 mock.doSomething(anyLong, null);
509                 result = 2L;
510             }
511         };
512 
513         assertEquals(1L, b.doSomething(1L, "A"));
514         assertEquals(2L, b.doSomething(2L, "B"));
515         assertEquals(2L, b.doSomething(1L, "c"));
516         assertEquals(1L, b.doSomething(2L, "A"));
517         assertEquals(2L, b.doSomething(0L, "B"));
518         assertEquals(2L, b.doSomething(0L, null));
519     }
520 
521     /**
522      * Record multiple non strict expectations with same delegate.
523      */
524     @Test
525     void recordMultipleNonStrictExpectationsWithSameDelegate() {
526         final Delegate<String> delegate = new Delegate<String>() {
527             @SuppressWarnings("unused")
528             boolean matches(String arg) {
529                 return !arg.isEmpty();
530             }
531         };
532 
533         new Expectations() {
534             {
535                 mock.doSomething(with(delegate));
536                 result = "first";
537             }
538         };
539 
540         assertEquals("first", mock.doSomething("test1"));
541 
542         new Expectations() {
543             {
544                 mock.doSomething(with(delegate));
545                 result = "second";
546             }
547         };
548 
549         assertEquals("second", mock.doSomething("test2"));
550     }
551 }