• 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 
6 package org.mockito.internal.verification.checkers;
7 
8 import static java.util.Arrays.asList;
9 
10 import java.util.List;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.rules.ExpectedException;
14 import org.mockito.Mock;
15 import org.mockito.exceptions.verification.WantedButNotInvoked;
16 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
17 import org.mockito.internal.invocation.InvocationBuilder;
18 import org.mockito.internal.invocation.InvocationMatcher;
19 import org.mockito.invocation.Invocation;
20 import org.mockitousage.IMethods;
21 import org.mockitoutil.TestBase;
22 
23 public class MissingInvocationCheckerTest extends TestBase {
24 
25 	private InvocationMatcher wanted;
26 	private List<Invocation> invocations;
27 
28 	@Mock
29 	private IMethods mock;
30 
31 	@Rule
32 	public ExpectedException exception = ExpectedException.none();
33 
34 	@Test
shouldPassBecauseActualInvocationFound()35 	public void shouldPassBecauseActualInvocationFound() {
36 		wanted = buildSimpleMethod().toInvocationMatcher();
37 		invocations = asList(buildSimpleMethod().toInvocation());
38 
39 		MissingInvocationChecker.checkMissingInvocation(invocations, wanted);
40 	}
41 
42 	@Test
shouldReportWantedButNotInvoked()43 	public void shouldReportWantedButNotInvoked() {
44 		wanted = buildSimpleMethod().toInvocationMatcher();
45 		invocations = asList(buildDifferentMethod().toInvocation());
46 
47 		exception.expect(WantedButNotInvoked.class);
48 		exception.expectMessage("Wanted but not invoked:");
49 		exception.expectMessage("mock.simpleMethod()");
50 		exception.expectMessage("However, there was exactly 1 interaction with this mock:");
51 		exception.expectMessage("mock.differentMethod();");
52 
53 		MissingInvocationChecker.checkMissingInvocation(invocations, wanted);
54 	}
55 
56 	@Test
shouldReportWantedInvocationDiffersFromActual()57 	public void shouldReportWantedInvocationDiffersFromActual() {
58 		wanted = buildIntArgMethod().arg(2222).toInvocationMatcher();
59 		invocations = asList(buildIntArgMethod().arg(1111).toInvocation());
60 
61 		exception.expect(ArgumentsAreDifferent.class);
62 
63 		exception.expectMessage("Argument(s) are different! Wanted:");
64 		exception.expectMessage("mock.intArgumentMethod(2222);");
65 		exception.expectMessage("Actual invocation has different arguments:");
66 		exception.expectMessage("mock.intArgumentMethod(1111);");
67 
68 		MissingInvocationChecker.checkMissingInvocation(invocations, wanted);
69 	}
70 
buildIntArgMethod()71 	private InvocationBuilder buildIntArgMethod() {
72 		return new InvocationBuilder().mock(mock).method("intArgumentMethod").argTypes(int.class);
73 	}
74 
buildSimpleMethod()75 	private InvocationBuilder buildSimpleMethod() {
76 		return new InvocationBuilder().mock(mock).simpleMethod();
77 	}
78 
buildDifferentMethod()79 	private InvocationBuilder buildDifferentMethod() {
80 		return new InvocationBuilder().mock(mock).differentMethod();
81 	}
82 }
83