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