• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.matchers;
6 
7 import static org.assertj.core.api.Assertions.assertThat;
8 import static org.assertj.core.api.Assertions.assertThatThrownBy;
9 import static org.junit.Assert.assertEquals;
10 import static org.junit.Assert.assertNotSame;
11 import static org.junit.Assert.fail;
12 import static org.mockito.AdditionalMatchers.and;
13 import static org.mockito.AdditionalMatchers.aryEq;
14 import static org.mockito.AdditionalMatchers.cmpEq;
15 import static org.mockito.AdditionalMatchers.eq;
16 import static org.mockito.AdditionalMatchers.find;
17 import static org.mockito.AdditionalMatchers.geq;
18 import static org.mockito.AdditionalMatchers.gt;
19 import static org.mockito.AdditionalMatchers.leq;
20 import static org.mockito.AdditionalMatchers.lt;
21 import static org.mockito.AdditionalMatchers.not;
22 import static org.mockito.AdditionalMatchers.or;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.ArgumentMatchers.nullable;
25 import static org.mockito.Mockito.any;
26 import static org.mockito.Mockito.anyBoolean;
27 import static org.mockito.Mockito.anyByte;
28 import static org.mockito.Mockito.anyChar;
29 import static org.mockito.Mockito.anyDouble;
30 import static org.mockito.Mockito.anyFloat;
31 import static org.mockito.Mockito.anyInt;
32 import static org.mockito.Mockito.anyLong;
33 import static org.mockito.Mockito.anyShort;
34 import static org.mockito.Mockito.anyString;
35 import static org.mockito.Mockito.contains;
36 import static org.mockito.Mockito.endsWith;
37 import static org.mockito.Mockito.isA;
38 import static org.mockito.Mockito.isNotNull;
39 import static org.mockito.Mockito.isNull;
40 import static org.mockito.Mockito.matches;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.notNull;
43 import static org.mockito.Mockito.same;
44 import static org.mockito.Mockito.startsWith;
45 import static org.mockito.Mockito.times;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
48 
49 import java.math.BigDecimal;
50 import java.util.ArrayList;
51 import java.util.List;
52 import java.util.RandomAccess;
53 import java.util.regex.Pattern;
54 
55 import org.junit.Test;
56 import org.mockito.ArgumentMatchers;
57 import org.mockito.Mockito;
58 import org.mockito.exceptions.verification.WantedButNotInvoked;
59 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent;
60 import org.mockitousage.IMethods;
61 import org.mockitoutil.TestBase;
62 
63 @SuppressWarnings("unchecked")
64 public class MatchersTest extends TestBase {
65     private IMethods mock = Mockito.mock(IMethods.class);
66 
67     @Test
and_overloaded()68     public void and_overloaded() {
69         when(mock.oneArg(and(eq(false), eq(false)))).thenReturn("0");
70         when(mock.oneArg(and(eq((byte) 1), eq((byte) 1)))).thenReturn("1");
71         when(mock.oneArg(and(eq('a'), eq('a')))).thenReturn("2");
72         when(mock.oneArg(and(eq(1D), eq(1D)))).thenReturn("3");
73         when(mock.oneArg(and(eq(1F), eq(1F)))).thenReturn("4");
74         when(mock.oneArg(and(eq(1), eq(1)))).thenReturn("5");
75         when(mock.oneArg(and(eq(1L), eq(1L)))).thenReturn("6");
76         when(mock.oneArg(and(eq((short) 1), eq((short) 1)))).thenReturn("7");
77         when(mock.oneArg(and(contains("a"), contains("d")))).thenReturn("8");
78         when(mock.oneArg(and(isA(Class.class), eq(Object.class)))).thenReturn("9");
79 
80         assertEquals("0", mock.oneArg(false));
81         assertEquals(null, mock.oneArg(true));
82 
83         assertEquals("1", mock.oneArg((byte) 1));
84         assertEquals("2", mock.oneArg('a'));
85         assertEquals("3", mock.oneArg(1D));
86         assertEquals("4", mock.oneArg(1F));
87         assertEquals("5", mock.oneArg(1));
88         assertEquals("6", mock.oneArg(1L));
89         assertEquals("7", mock.oneArg((short) 1));
90 
91         assertEquals("8", mock.oneArg("abcde"));
92         assertEquals(null, mock.oneArg("aaaaa"));
93 
94         assertEquals("9", mock.oneArg(Object.class));
95     }
96 
97     @Test
or_overloaded()98     public void or_overloaded() {
99         when(mock.oneArg(or(eq(false), eq(true)))).thenReturn("0");
100         when(mock.oneArg(or(eq((byte) 1), eq((byte) 2)))).thenReturn("1");
101         when(mock.oneArg(or(eq((char) 1), eq((char) 2)))).thenReturn("2");
102         when(mock.oneArg(or(eq(1D), eq(2D)))).thenReturn("3");
103         when(mock.oneArg(or(eq(1F), eq(2F)))).thenReturn("4");
104         when(mock.oneArg(or(eq(1), eq(2)))).thenReturn("5");
105         when(mock.oneArg(or(eq(1L), eq(2L)))).thenReturn("6");
106         when(mock.oneArg(or(eq((short) 1), eq((short) 2)))).thenReturn("7");
107         when(mock.oneArg(or(eq("asd"), eq("jkl")))).thenReturn("8");
108         when(mock.oneArg(or(eq(this.getClass()), eq(Object.class)))).thenReturn("9");
109 
110         assertEquals("0", mock.oneArg(true));
111         assertEquals("0", mock.oneArg(false));
112 
113         assertEquals("1", mock.oneArg((byte) 2));
114         assertEquals("2", mock.oneArg((char) 1));
115         assertEquals("3", mock.oneArg(2D));
116         assertEquals("4", mock.oneArg(1F));
117         assertEquals("5", mock.oneArg(2));
118         assertEquals("6", mock.oneArg(1L));
119         assertEquals("7", mock.oneArg((short) 1));
120 
121         assertEquals("8", mock.oneArg("jkl"));
122         assertEquals("8", mock.oneArg("asd"));
123         assertEquals(null, mock.oneArg("asdjkl"));
124 
125         assertEquals("9", mock.oneArg(Object.class));
126         assertEquals(null, mock.oneArg(String.class));
127     }
128 
129     @Test
not_overloaded()130     public void not_overloaded() {
131         when(mock.oneArg(not(eq(false)))).thenReturn("0");
132         when(mock.oneArg(not(eq((byte) 1)))).thenReturn("1");
133         when(mock.oneArg(not(eq('a')))).thenReturn("2");
134         when(mock.oneArg(not(eq(1D)))).thenReturn("3");
135         when(mock.oneArg(not(eq(1F)))).thenReturn("4");
136         when(mock.oneArg(not(eq(1)))).thenReturn("5");
137         when(mock.oneArg(not(eq(1L)))).thenReturn("6");
138         when(mock.oneArg(not(eq((short) 1)))).thenReturn("7");
139         when(mock.oneArg(not(contains("a")))).thenReturn("8");
140         when(mock.oneArg(not(isA(Class.class)))).thenReturn("9");
141 
142         assertEquals("0", mock.oneArg(true));
143         assertEquals(null, mock.oneArg(false));
144 
145         assertEquals("1", mock.oneArg((byte) 2));
146         assertEquals("2", mock.oneArg('b'));
147         assertEquals("3", mock.oneArg(2D));
148         assertEquals("4", mock.oneArg(2F));
149         assertEquals("5", mock.oneArg(2));
150         assertEquals("6", mock.oneArg(2L));
151         assertEquals("7", mock.oneArg((short) 2));
152         assertEquals("8", mock.oneArg("bcde"));
153 
154         assertEquals("9", mock.oneArg(new Object()));
155         assertEquals(null, mock.oneArg(Class.class));
156     }
157 
158     @Test
less_or_equal_overloaded()159     public void less_or_equal_overloaded() {
160         when(mock.oneArg(leq((byte) 1))).thenReturn("1");
161         when(mock.oneArg(leq(1D))).thenReturn("3");
162         when(mock.oneArg(leq(1F))).thenReturn("4");
163         when(mock.oneArg(leq(1))).thenReturn("5");
164         when(mock.oneArg(leq(1L))).thenReturn("6");
165         when(mock.oneArg(leq((short) 1))).thenReturn("7");
166         when(mock.oneArg(leq(new BigDecimal("1")))).thenReturn("8");
167 
168         assertEquals("1", mock.oneArg((byte) 1));
169         assertEquals(null, mock.oneArg((byte) 2));
170 
171         assertEquals("3", mock.oneArg(1D));
172         assertEquals("7", mock.oneArg((short) 0));
173         assertEquals("4", mock.oneArg(-5F));
174         assertEquals("5", mock.oneArg(-2));
175         assertEquals("6", mock.oneArg(-3L));
176 
177         assertEquals("8", mock.oneArg(new BigDecimal("0.5")));
178         assertEquals(null, mock.oneArg(new BigDecimal("1.1")));
179     }
180 
181     @Test
less_than_overloaded()182     public void less_than_overloaded() {
183         when(mock.oneArg(lt((byte) 1))).thenReturn("1");
184         when(mock.oneArg(lt(1D))).thenReturn("3");
185         when(mock.oneArg(lt(1F))).thenReturn("4");
186         when(mock.oneArg(lt(1))).thenReturn("5");
187         when(mock.oneArg(lt(1L))).thenReturn("6");
188         when(mock.oneArg(lt((short) 1))).thenReturn("7");
189         when(mock.oneArg(lt(new BigDecimal("1")))).thenReturn("8");
190 
191         assertEquals("1", mock.oneArg((byte) 0));
192         assertEquals(null, mock.oneArg((byte) 1));
193 
194         assertEquals("3", mock.oneArg(0D));
195         assertEquals("7", mock.oneArg((short) 0));
196         assertEquals("4", mock.oneArg(-4F));
197         assertEquals("5", mock.oneArg(-34));
198         assertEquals("6", mock.oneArg(-6L));
199 
200         assertEquals("8", mock.oneArg(new BigDecimal("0.5")));
201         assertEquals(null, mock.oneArg(new BigDecimal("23")));
202     }
203 
204     @Test
greater_or_equal_matcher_overloaded()205     public void greater_or_equal_matcher_overloaded() {
206         when(mock.oneArg(geq((byte) 1))).thenReturn("1");
207         when(mock.oneArg(geq(1D))).thenReturn("3");
208         when(mock.oneArg(geq(1F))).thenReturn("4");
209         when(mock.oneArg(geq(1))).thenReturn("5");
210         when(mock.oneArg(geq(1L))).thenReturn("6");
211         when(mock.oneArg(geq((short) 1))).thenReturn("7");
212         when(mock.oneArg(geq(new BigDecimal("1")))).thenReturn("8");
213 
214         assertEquals("1", mock.oneArg((byte) 2));
215         assertEquals(null, mock.oneArg((byte) 0));
216 
217         assertEquals("3", mock.oneArg(1D));
218         assertEquals("7", mock.oneArg((short) 2));
219         assertEquals("4", mock.oneArg(3F));
220         assertEquals("5", mock.oneArg(4));
221         assertEquals("6", mock.oneArg(5L));
222 
223         assertEquals("8", mock.oneArg(new BigDecimal("1.00")));
224         assertEquals(null, mock.oneArg(new BigDecimal("0.9")));
225     }
226 
227     @Test
greater_than_matcher_overloaded()228     public void greater_than_matcher_overloaded() {
229         when(mock.oneArg(gt((byte) 1))).thenReturn("1");
230         when(mock.oneArg(gt(1D))).thenReturn("3");
231         when(mock.oneArg(gt(1F))).thenReturn("4");
232         when(mock.oneArg(gt(1))).thenReturn("5");
233         when(mock.oneArg(gt(1L))).thenReturn("6");
234         when(mock.oneArg(gt((short) 1))).thenReturn("7");
235         when(mock.oneArg(gt(new BigDecimal("1")))).thenReturn("8");
236 
237         assertEquals("1", mock.oneArg((byte) 2));
238         assertEquals(null, mock.oneArg((byte) 1));
239 
240         assertEquals("3", mock.oneArg(2D));
241         assertEquals("7", mock.oneArg((short) 2));
242         assertEquals("4", mock.oneArg(3F));
243         assertEquals("5", mock.oneArg(2));
244         assertEquals("6", mock.oneArg(5L));
245 
246         assertEquals("8", mock.oneArg(new BigDecimal("1.5")));
247         assertEquals(null, mock.oneArg(new BigDecimal("0.9")));
248     }
249 
250     @Test
compare_to_matcher()251     public void compare_to_matcher() {
252         when(mock.oneArg(cmpEq(new BigDecimal("1.5")))).thenReturn("0");
253 
254         assertEquals("0", mock.oneArg(new BigDecimal("1.50")));
255         assertEquals(null, mock.oneArg(new BigDecimal("1.51")));
256     }
257 
258     @Test
any_String_matcher()259     public void any_String_matcher() {
260         when(mock.oneArg(anyString())).thenReturn("matched");
261 
262         assertEquals("matched", mock.oneArg(""));
263         assertEquals("matched", mock.oneArg("any string"));
264         assertEquals(null, mock.oneArg((String) null));
265     }
266 
267     @Test
any_matcher()268     public void any_matcher() {
269         when(mock.forObject(any())).thenReturn("matched");
270 
271         assertEquals("matched", mock.forObject(123));
272         assertEquals("matched", mock.forObject("any string"));
273         assertEquals("matched", mock.forObject("any string"));
274         assertEquals("matched", mock.forObject(null));
275     }
276 
277     @Test
any_T_matcher()278     public void any_T_matcher() {
279         when(mock.oneArg(anyBoolean())).thenReturn("0");
280         when(mock.oneArg(anyByte())).thenReturn("1");
281         when(mock.oneArg(anyChar())).thenReturn("2");
282         when(mock.oneArg(anyDouble())).thenReturn("3");
283         when(mock.oneArg(anyFloat())).thenReturn("4");
284         when(mock.oneArg(anyInt())).thenReturn("5");
285         when(mock.oneArg(anyLong())).thenReturn("6");
286         when(mock.oneArg(anyShort())).thenReturn("7");
287         when(mock.oneArg(any(RandomAccess.class))).thenReturn("8");
288 
289         assertEquals("0", mock.oneArg(true));
290         assertEquals("0", mock.oneArg(false));
291 
292         assertEquals("1", mock.oneArg((byte) 1));
293         assertEquals("2", mock.oneArg((char) 1));
294         assertEquals("3", mock.oneArg(1D));
295         assertEquals("4", mock.oneArg(889F));
296         assertEquals("5", mock.oneArg(1));
297         assertEquals("6", mock.oneArg(1L));
298         assertEquals("7", mock.oneArg((short) 1));
299 
300         assertEquals("8", mock.oneArg(new ArrayList()));
301     }
302 
303     @Test
should_array_equals_deal_with_null_array()304     public void should_array_equals_deal_with_null_array() throws Exception {
305         Object[] nullArray = null;
306         when(mock.oneArray(aryEq(nullArray))).thenReturn("null");
307 
308         assertEquals("null", mock.oneArray(nullArray));
309 
310         mock = mock(IMethods.class);
311 
312         try {
313             verify(mock).oneArray(aryEq(nullArray));
314             fail();
315         } catch (WantedButNotInvoked e) {
316             assertThat(e).hasMessageContaining("oneArray(null)");
317         }
318     }
319 
320     @Test
should_use_smart_equals_for_arrays()321     public void should_use_smart_equals_for_arrays() throws Exception {
322         // issue 143
323         mock.arrayMethod(new String[] {"one"});
324         verify(mock).arrayMethod(eq(new String[] {"one"}));
325         verify(mock).arrayMethod(new String[] {"one"});
326     }
327 
328     @Test
should_use_smart_equals_for_primitive_arrays()329     public void should_use_smart_equals_for_primitive_arrays() throws Exception {
330         // issue 143
331         mock.objectArgMethod(new int[] {1, 2});
332         verify(mock).objectArgMethod(eq(new int[] {1, 2}));
333         verify(mock).objectArgMethod(new int[] {1, 2});
334     }
335 
336     @SuppressWarnings("ReturnValueIgnored")
337     @Test
338     public void
array_equals_should_throw_ArgumentsAreDifferentException_for_non_matching_arguments()339             array_equals_should_throw_ArgumentsAreDifferentException_for_non_matching_arguments() {
340         List<Object> list = Mockito.mock(List.class);
341 
342         list.add("test"); // testing fix for issue 20
343         list.contains(new Object[] {"1"});
344 
345         assertThatThrownBy(
346                         () -> {
347                             Mockito.verify(list).contains(new Object[] {"1", "2", "3"});
348                         })
349                 .isInstanceOf(ArgumentsAreDifferent.class)
350                 .hasMessageContainingAll(
351                         "Argument(s) are different! Wanted:",
352                         "list.contains([\"1\", \"2\", \"3\"]);",
353                         "Actual invocations have different arguments:",
354                         "list.add(\"test\");");
355     }
356 
357     @Test
array_equals_matcher()358     public void array_equals_matcher() {
359         when(mock.oneArray(aryEq(new boolean[] {true, false, false}))).thenReturn("0");
360         when(mock.oneArray(aryEq(new byte[] {1}))).thenReturn("1");
361         when(mock.oneArray(aryEq(new char[] {1}))).thenReturn("2");
362         when(mock.oneArray(aryEq(new double[] {1}))).thenReturn("3");
363         when(mock.oneArray(aryEq(new float[] {1}))).thenReturn("4");
364         when(mock.oneArray(aryEq(new int[] {1}))).thenReturn("5");
365         when(mock.oneArray(aryEq(new long[] {1}))).thenReturn("6");
366         when(mock.oneArray(aryEq(new short[] {1}))).thenReturn("7");
367         when(mock.oneArray(aryEq(new String[] {"Test"}))).thenReturn("8");
368         when(mock.oneArray(aryEq(new Object[] {"Test", new Integer(4)}))).thenReturn("9");
369 
370         assertEquals("0", mock.oneArray(new boolean[] {true, false, false}));
371         assertEquals("1", mock.oneArray(new byte[] {1}));
372         assertEquals("2", mock.oneArray(new char[] {1}));
373         assertEquals("3", mock.oneArray(new double[] {1}));
374         assertEquals("4", mock.oneArray(new float[] {1}));
375         assertEquals("5", mock.oneArray(new int[] {1}));
376         assertEquals("6", mock.oneArray(new long[] {1}));
377         assertEquals("7", mock.oneArray(new short[] {1}));
378         assertEquals("8", mock.oneArray(new String[] {"Test"}));
379         assertEquals("9", mock.oneArray(new Object[] {"Test", new Integer(4)}));
380 
381         assertEquals(null, mock.oneArray(new Object[] {"Test", new Integer(999)}));
382         assertEquals(null, mock.oneArray(new Object[] {"Test", new Integer(4), "x"}));
383 
384         assertEquals(null, mock.oneArray(new boolean[] {true, false}));
385         assertEquals(null, mock.oneArray(new boolean[] {true, true, false}));
386     }
387 
388     @Test
greater_or_equal_matcher()389     public void greater_or_equal_matcher() {
390         when(mock.oneArg(geq(7))).thenReturn(">= 7");
391         when(mock.oneArg(lt(7))).thenReturn("< 7");
392 
393         assertEquals(">= 7", mock.oneArg(7));
394         assertEquals(">= 7", mock.oneArg(8));
395         assertEquals(">= 7", mock.oneArg(9));
396 
397         assertEquals("< 7", mock.oneArg(6));
398         assertEquals("< 7", mock.oneArg(6));
399     }
400 
401     @Test
greater_than_matcher()402     public void greater_than_matcher() {
403         when(mock.oneArg(gt(7))).thenReturn("> 7");
404         when(mock.oneArg(leq(7))).thenReturn("<= 7");
405 
406         assertEquals("> 7", mock.oneArg(8));
407         assertEquals("> 7", mock.oneArg(9));
408         assertEquals("> 7", mock.oneArg(10));
409 
410         assertEquals("<= 7", mock.oneArg(7));
411         assertEquals("<= 7", mock.oneArg(6));
412     }
413 
414     @Test
less_or_equal_matcher()415     public void less_or_equal_matcher() {
416         when(mock.oneArg(leq(7))).thenReturn("<= 7");
417         when(mock.oneArg(gt(7))).thenReturn("> 7");
418 
419         assertEquals("<= 7", mock.oneArg(7));
420         assertEquals("<= 7", mock.oneArg(6));
421         assertEquals("<= 7", mock.oneArg(5));
422 
423         assertEquals("> 7", mock.oneArg(8));
424         assertEquals("> 7", mock.oneArg(9));
425     }
426 
427     @Test
less_than_matcher()428     public void less_than_matcher() {
429         when(mock.oneArg(lt(7))).thenReturn("< 7");
430         when(mock.oneArg(geq(7))).thenReturn(">= 7");
431 
432         assertEquals("< 7", mock.oneArg(5));
433         assertEquals("< 7", mock.oneArg(6));
434         assertEquals("< 7", mock.oneArg(4));
435 
436         assertEquals(">= 7", mock.oneArg(7));
437         assertEquals(">= 7", mock.oneArg(8));
438     }
439 
440     @Test
or_matcher()441     public void or_matcher() {
442         when(mock.oneArg(anyInt())).thenReturn("other");
443         when(mock.oneArg(or(eq(7), eq(9)))).thenReturn("7 or 9");
444 
445         assertEquals("other", mock.oneArg(10));
446         assertEquals("7 or 9", mock.oneArg(7));
447         assertEquals("7 or 9", mock.oneArg(9));
448     }
449 
450     @Test
null_matcher()451     public void null_matcher() {
452         when(mock.threeArgumentMethod(eq(1), isNull(), eq(""))).thenReturn("1");
453         when(mock.threeArgumentMethod(eq(1), not(isNull()), eq(""))).thenReturn("2");
454 
455         assertEquals("1", mock.threeArgumentMethod(1, null, ""));
456         assertEquals("2", mock.threeArgumentMethod(1, new Object(), ""));
457     }
458 
459     @Test
null_matcher_for_primitive_wrappers()460     public void null_matcher_for_primitive_wrappers() {
461         when(mock.forBoolean(ArgumentMatchers.<Boolean>isNull())).thenReturn("ok");
462         when(mock.forInteger(ArgumentMatchers.<Integer>isNull())).thenReturn("ok");
463         when(mock.forLong(ArgumentMatchers.<Long>isNull())).thenReturn("ok");
464         when(mock.forByte(ArgumentMatchers.<Byte>isNull())).thenReturn("ok");
465         when(mock.forShort(ArgumentMatchers.<Short>isNull())).thenReturn("ok");
466         when(mock.forCharacter(ArgumentMatchers.<Character>isNull())).thenReturn("ok");
467         when(mock.forDouble(ArgumentMatchers.<Double>isNull())).thenReturn("ok");
468         when(mock.forFloat(ArgumentMatchers.<Float>isNull())).thenReturn("ok");
469 
470         assertEquals("ok", mock.forBoolean(null));
471         assertEquals("ok", mock.forInteger(null));
472         assertEquals("ok", mock.forLong(null));
473         assertEquals("ok", mock.forByte(null));
474         assertEquals("ok", mock.forShort(null));
475         assertEquals("ok", mock.forCharacter(null));
476         assertEquals("ok", mock.forDouble(null));
477         assertEquals("ok", mock.forFloat(null));
478     }
479 
480     @Test
not_null_matcher()481     public void not_null_matcher() {
482         when(mock.threeArgumentMethod(eq(1), notNull(), eq(""))).thenReturn("1");
483         when(mock.threeArgumentMethod(eq(1), not(isNotNull()), eq(""))).thenReturn("2");
484 
485         assertEquals("1", mock.threeArgumentMethod(1, new Object(), ""));
486         assertEquals("2", mock.threeArgumentMethod(1, null, ""));
487     }
488 
489     @Test
find_matcher()490     public void find_matcher() {
491         when(mock.oneArg(find("([a-z]+)\\d"))).thenReturn("1");
492 
493         assertEquals("1", mock.oneArg("ab12"));
494         assertEquals(null, mock.oneArg("12345"));
495         assertEquals(null, mock.oneArg((Object) null));
496     }
497 
498     @Test
matches_matcher()499     public void matches_matcher() {
500         when(mock.oneArg(matches("[a-z]+\\d\\d"))).thenReturn("1");
501         when(mock.oneArg(matches("\\d\\d\\d"))).thenReturn("2");
502 
503         assertEquals("1", mock.oneArg("a12"));
504         assertEquals("2", mock.oneArg("131"));
505         assertEquals(null, mock.oneArg("blah"));
506     }
507 
508     @Test
matches_Pattern_matcher()509     public void matches_Pattern_matcher() {
510         when(mock.oneArg(matches(Pattern.compile("[a-z]+\\d\\d")))).thenReturn("1");
511         when(mock.oneArg(matches(Pattern.compile("\\d\\d\\d")))).thenReturn("2");
512 
513         assertEquals("1", mock.oneArg("a12"));
514         assertEquals("2", mock.oneArg("131"));
515         assertEquals(null, mock.oneArg("blah"));
516     }
517 
518     @Test
matches_Pattern_matcher_in_subregion()519     public void matches_Pattern_matcher_in_subregion() {
520         when(mock.oneArg(matches(Pattern.compile("[a-z]")))).thenReturn("1");
521 
522         assertEquals("1", mock.oneArg("3a45"));
523         assertEquals(null, mock.oneArg("3445"));
524     }
525 
526     @Test
contains_matcher()527     public void contains_matcher() {
528         when(mock.oneArg(contains("ell"))).thenReturn("1");
529         when(mock.oneArg(contains("ld"))).thenReturn("2");
530 
531         assertEquals("1", mock.oneArg("hello"));
532         assertEquals("2", mock.oneArg("world"));
533         assertEquals(null, mock.oneArg("xlx"));
534     }
535 
536     @Test
starts_with_matcher()537     public void starts_with_matcher() {
538         when(mock.oneArg(startsWith("ab"))).thenReturn("1");
539         when(mock.oneArg(startsWith("bc"))).thenReturn("2");
540 
541         assertEquals("1", mock.oneArg("ab quake"));
542         assertEquals("2", mock.oneArg("bc quake"));
543         assertEquals(null, mock.oneArg("ba quake"));
544     }
545 
546     @Test
ends_with_matcher()547     public void ends_with_matcher() {
548         when(mock.oneArg(endsWith("ab"))).thenReturn("1");
549         when(mock.oneArg(endsWith("bc"))).thenReturn("2");
550 
551         assertEquals("1", mock.oneArg("xab"));
552         assertEquals("2", mock.oneArg("xbc"));
553         assertEquals(null, mock.oneArg("ac"));
554     }
555 
556     @Test
delta_matcher()557     public void delta_matcher() {
558         when(mock.oneArg(eq(1.0D, 0.1D))).thenReturn("1");
559         when(mock.oneArg(eq(2.0D, 0.1D))).thenReturn("2");
560         when(mock.oneArg(eq(1.0F, 0.1F))).thenReturn("3");
561         when(mock.oneArg(eq(2.0F, 0.1F))).thenReturn("4");
562         when(mock.oneArg(eq(2.0F, 0.1F))).thenReturn("4");
563 
564         assertEquals("1", mock.oneArg(1.0));
565         assertEquals("1", mock.oneArg(0.91));
566         assertEquals("1", mock.oneArg(1.09));
567         assertEquals("2", mock.oneArg(2.0));
568 
569         assertEquals("3", mock.oneArg(1.0F));
570         assertEquals("3", mock.oneArg(0.91F));
571         assertEquals("3", mock.oneArg(1.09F));
572         assertEquals("4", mock.oneArg(2.1F));
573 
574         assertEquals(null, mock.oneArg(2.2F));
575     }
576 
577     @Test
delta_matcher_prints_itself()578     public void delta_matcher_prints_itself() {
579         try {
580             verify(mock).oneArg(eq(1.0D, 0.1D));
581             fail();
582         } catch (WantedButNotInvoked e) {
583             assertThat(e).hasMessageContaining("eq(1.0, 0.1)");
584         }
585     }
586 
587     @Test
same_matcher()588     public void same_matcher() {
589         Object one = new String("1243");
590         Object two = new String("1243");
591         Object three = new String("1243");
592 
593         assertNotSame(one, two);
594         assertEquals(one, two);
595         assertEquals(two, three);
596 
597         when(mock.oneArg(same(one))).thenReturn("1");
598         when(mock.oneArg(same(two))).thenReturn("2");
599 
600         assertEquals("1", mock.oneArg(one));
601         assertEquals("2", mock.oneArg(two));
602         assertEquals(null, mock.oneArg(three));
603     }
604 
605     @Test
eq_matcher_and_nulls()606     public void eq_matcher_and_nulls() {
607         mock.simpleMethod((Object) null);
608 
609         verify(mock).simpleMethod(Mockito.<Object>eq(null));
610     }
611 
612     @Test
same_matcher_and_nulls()613     public void same_matcher_and_nulls() {
614         mock.simpleMethod((Object) null);
615 
616         verify(mock).simpleMethod(Mockito.<Object>same(null));
617     }
618 
619     @Test
nullable_matcher()620     public void nullable_matcher() throws Exception {
621         // imagine a Stream.of(...).map(c -> mock.oneArg(c))...
622         mock.oneArg((Character) null);
623         mock.oneArg(Character.valueOf('\u20AC'));
624 
625         verify(mock, times(2)).oneArg(nullable(Character.class));
626     }
627 }
628