Home | History | Annotate | Download | only in checkers
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.internal.verification.checkers;
      6 
      7 import org.junit.Rule;
      8 import org.junit.Test;
      9 import org.junit.rules.ExpectedException;
     10 import org.mockito.exceptions.verification.TooLittleActualInvocations;
     11 import org.mockito.exceptions.verification.VerificationInOrderFailure;
     12 import org.mockito.internal.invocation.InvocationBuilder;
     13 import org.mockito.internal.invocation.InvocationMatcher;
     14 import org.mockito.internal.verification.InOrderContextImpl;
     15 import org.mockito.internal.verification.api.InOrderContext;
     16 import org.mockito.invocation.Invocation;
     17 
     18 import static java.util.Arrays.asList;
     19 import static org.assertj.core.api.Assertions.assertThat;
     20 import static org.mockito.internal.verification.checkers.AtLeastXNumberOfInvocationsChecker.checkAtLeastNumberOfInvocations;
     21 
     22 public class AtLeastXNumberOfInvocationsCheckerTest   {
     23 
     24     @Rule
     25     public ExpectedException exception = ExpectedException.none();
     26 
     27     @Test
     28     public void shouldMarkActualInvocationsAsVerifiedInOrder() {
     29         InOrderContext context = new InOrderContextImpl();
     30         //given
     31         Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
     32         Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
     33 
     34         //when
     35         checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 1, context);
     36 
     37         //then
     38         assertThat(invocation.isVerified()).isTrue();
     39     }
     40 
     41     @Test
     42     public void shouldReportTooLittleInvocationsInOrder() {
     43         InOrderContext context = new InOrderContextImpl();
     44         //given
     45         Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
     46         Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
     47 
     48         exception.expect(VerificationInOrderFailure.class);
     49         exception.expectMessage("iMethods.simpleMethod()");
     50         exception.expectMessage("Wanted *at least* 2 times");
     51         exception.expectMessage("But was 1 time");
     52 
     53         //when
     54         checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 2, context);
     55 
     56 
     57     }
     58 
     59     @Test
     60     public void shouldMarkActualInvocationsAsVerified() {
     61         //given
     62         Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
     63         Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
     64 
     65         //when
     66         checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 1);
     67 
     68         //then
     69         assertThat(invocation.isVerified()).isTrue();
     70     }
     71 
     72     @Test
     73     public void shouldReportTooLittleInvocations() {
     74         //given
     75         Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
     76         Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
     77 
     78         exception.expect(TooLittleActualInvocations.class);
     79         exception.expectMessage("iMethods.simpleMethod()");
     80         exception.expectMessage("Wanted *at least* 2 times");
     81         exception.expectMessage("But was 1 time");
     82 
     83         //when
     84         checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 2);
     85     }
     86 }
     87