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