• 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.verification;
6 
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertNotSame;
9 import static org.junit.Assert.fail;
10 import static org.mockito.Mockito.times;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.ArgumentMatchers.same;
13 import static org.mockito.ArgumentMatchers.anyInt;
14 import static org.mockito.ArgumentMatchers.isA;
15 import static org.mockito.ArgumentMatchers.contains;
16 import static org.mockito.AdditionalMatchers.geq;
17 import static org.mockito.AdditionalMatchers.leq;
18 import static org.mockito.AdditionalMatchers.and;
19 
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.mockito.exceptions.verification.WantedButNotInvoked;
24 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent;
25 import org.mockitousage.IMethods;
26 import org.mockitoutil.TestBase;
27 
28 public class VerificationUsingMatchersTest extends TestBase {
29 
30     private IMethods mock;
31 
32     @Before
setUp()33     public void setUp() {
34         mock = Mockito.mock(IMethods.class);
35     }
36 
37     @Test
shouldVerifyExactNumberOfInvocationsUsingMatcher()38     public void shouldVerifyExactNumberOfInvocationsUsingMatcher() {
39         mock.simpleMethod(1);
40         mock.simpleMethod(2);
41         mock.simpleMethod(3);
42 
43         verify(mock, times(3)).simpleMethod(anyInt());
44     }
45 
46     @Test
shouldVerifyUsingSameMatcher()47     public void shouldVerifyUsingSameMatcher() {
48         Object one = new String("1243");
49         Object two = new String("1243");
50         Object three = new String("1243");
51 
52         assertNotSame(one, two);
53         assertEquals(one, two);
54         assertEquals(two, three);
55 
56         mock.oneArg(one);
57         mock.oneArg(two);
58 
59         verify(mock).oneArg(same(one));
60         verify(mock, times(2)).oneArg(two);
61 
62         try {
63             verify(mock).oneArg(same(three));
64             fail();
65         } catch (WantedButNotInvoked e) {
66         }
67     }
68 
69     @Test
shouldVerifyUsingMixedMatchers()70     public void shouldVerifyUsingMixedMatchers() {
71         mock.threeArgumentMethod(11, "", "01234");
72 
73         try {
74             verify(mock)
75                     .threeArgumentMethod(and(geq(7), leq(10)), isA(String.class), contains("123"));
76             fail();
77         } catch (ArgumentsAreDifferent e) {
78         }
79 
80         mock.threeArgumentMethod(8, new Object(), "01234");
81 
82         try {
83             verify(mock)
84                     .threeArgumentMethod(and(geq(7), leq(10)), isA(String.class), contains("123"));
85             fail();
86         } catch (ArgumentsAreDifferent e) {
87         }
88 
89         mock.threeArgumentMethod(8, "", "no match");
90 
91         try {
92             verify(mock)
93                     .threeArgumentMethod(and(geq(7), leq(10)), isA(String.class), contains("123"));
94             fail();
95         } catch (ArgumentsAreDifferent e) {
96         }
97 
98         mock.threeArgumentMethod(8, "", "123");
99 
100         verify(mock).threeArgumentMethod(and(geq(7), leq(10)), isA(String.class), contains("123"));
101     }
102 }
103