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