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.mockito.internal.verification.checkers; 7 8 import org.junit.Before; 9 import org.junit.Rule; 10 import org.junit.Test; 11 import org.junit.rules.ExpectedException; 12 import org.mockito.Mock; 13 import org.mockito.exceptions.verification.VerificationInOrderFailure; 14 import org.mockito.exceptions.verification.WantedButNotInvoked; 15 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; 16 import org.mockito.internal.invocation.InvocationBuilder; 17 import org.mockito.internal.invocation.InvocationMatcher; 18 import org.mockito.internal.progress.VerificationModeBuilder; 19 import org.mockito.internal.verification.InOrderContextImpl; 20 import org.mockito.internal.verification.api.InOrderContext; 21 import org.mockito.invocation.Invocation; 22 import org.mockito.junit.MockitoJUnit; 23 import org.mockito.junit.MockitoRule; 24 import org.mockitousage.IMethods; 25 26 import java.util.List; 27 28 import static java.util.Arrays.asList; 29 import static org.mockito.internal.verification.checkers.MissingInvocationChecker.checkMissingInvocation; 30 31 public class MissingInvocationInOrderCheckerTest { 32 33 private InvocationMatcher wanted; 34 private List<Invocation> invocations; 35 36 @Mock 37 private IMethods mock; 38 39 @Rule 40 public ExpectedException exception = ExpectedException.none(); 41 42 @Rule 43 public MockitoRule mockitoRule = MockitoJUnit.rule(); 44 45 private InOrderContext context = new InOrderContextImpl(); 46 47 @Before setup()48 public void setup() { 49 } 50 51 @Test shouldPassWhenMatchingInteractionFound()52 public void shouldPassWhenMatchingInteractionFound() throws Exception { 53 54 invocations = asList(buildSimpleMethod().toInvocation()); 55 wanted = buildSimpleMethod().toInvocationMatcher(); 56 57 checkMissingInvocation(invocations, wanted, context); 58 } 59 60 @Test shouldReportWantedButNotInvoked()61 public void shouldReportWantedButNotInvoked() throws Exception { 62 invocations = asList(buildDifferentMethod().toInvocation()); 63 wanted = buildSimpleMethod().toInvocationMatcher(); 64 65 exception.expect(WantedButNotInvoked.class); 66 exception.expectMessage("Wanted but not invoked:"); 67 exception.expectMessage("mock.simpleMethod()"); 68 69 checkMissingInvocation(invocations, wanted, context); 70 } 71 72 @Test shouldReportArgumentsAreDifferent()73 public void shouldReportArgumentsAreDifferent() throws Exception { 74 invocations = asList(buildIntArgMethod().arg(1111).toInvocation()); 75 wanted = buildIntArgMethod().arg(2222).toInvocationMatcher(); 76 77 exception.expect(ArgumentsAreDifferent.class); 78 79 exception.expectMessage("Argument(s) are different! Wanted:"); 80 exception.expectMessage("mock.intArgumentMethod(2222);"); 81 exception.expectMessage("Actual invocation has different arguments:"); 82 exception.expectMessage("mock.intArgumentMethod(1111);"); 83 84 checkMissingInvocation(invocations, wanted, context); 85 86 } 87 88 @Test shouldReportWantedDiffersFromActual()89 public void shouldReportWantedDiffersFromActual() throws Exception { 90 91 Invocation invocation1 = buildIntArgMethod().arg(1111).toInvocation(); 92 Invocation invocation2 = buildIntArgMethod().arg(2222).toInvocation(); 93 94 context.markVerified(invocation2); 95 invocations = asList(invocation1,invocation2); 96 wanted = buildIntArgMethod().arg(2222).toInvocationMatcher(); 97 98 exception.expect(VerificationInOrderFailure.class); 99 100 exception.expectMessage("Verification in order failure"); 101 exception.expectMessage("Wanted but not invoked:"); 102 exception.expectMessage("mock.intArgumentMethod(2222);"); 103 exception.expectMessage("Wanted anywhere AFTER following interaction:"); 104 exception.expectMessage("mock.intArgumentMethod(2222);"); 105 106 checkMissingInvocation(invocations, wanted, context); 107 } 108 buildIntArgMethod()109 private InvocationBuilder buildIntArgMethod() { 110 return new InvocationBuilder().mock(mock).method("intArgumentMethod").argTypes(int.class); 111 } 112 buildSimpleMethod()113 private InvocationBuilder buildSimpleMethod() { 114 return new InvocationBuilder().mock(mock).simpleMethod(); 115 } 116 buildDifferentMethod()117 private InvocationBuilder buildDifferentMethod() { 118 return new InvocationBuilder().mock(mock).differentMethod(); 119 } 120 121 122 } 123