12637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin/*
22637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin * Copyright (c) 2007 Mockito contributors
32637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin * This program is made available under the terms of the MIT License.
42637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin */
52637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
62637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinpackage org.mockito.internal.verification.checkers;
72637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
82637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport static java.util.Arrays.asList;
92637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
102637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport java.util.List;
112637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.junit.Rule;
122637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.junit.Test;
132637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.junit.rules.ExpectedException;
142637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.Mock;
152637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.exceptions.verification.WantedButNotInvoked;
162637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
172637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.internal.invocation.InvocationBuilder;
182637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.internal.invocation.InvocationMatcher;
192637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockito.invocation.Invocation;
202637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockitousage.IMethods;
212637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport org.mockitoutil.TestBase;
222637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
232637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinpublic class MissingInvocationCheckerTest extends TestBase {
242637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
252637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	private InvocationMatcher wanted;
262637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	private List<Invocation> invocations;
272637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
282637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	@Mock
292637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	private IMethods mock;
302637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
312637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	@Rule
322637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	public ExpectedException exception = ExpectedException.none();
332637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
342637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	@Test
352637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	public void shouldPassBecauseActualInvocationFound() {
362637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		wanted = buildSimpleMethod().toInvocationMatcher();
372637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		invocations = asList(buildSimpleMethod().toInvocation());
382637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
392637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		MissingInvocationChecker.checkMissingInvocation(invocations, wanted);
402637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	}
412637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
422637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	@Test
432637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	public void shouldReportWantedButNotInvoked() {
442637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		wanted = buildSimpleMethod().toInvocationMatcher();
452637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		invocations = asList(buildDifferentMethod().toInvocation());
462637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
472637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expect(WantedButNotInvoked.class);
482637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("Wanted but not invoked:");
492637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("mock.simpleMethod()");
502637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("However, there was exactly 1 interaction with this mock:");
512637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("mock.differentMethod();");
522637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
532637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		MissingInvocationChecker.checkMissingInvocation(invocations, wanted);
542637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	}
552637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
562637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	@Test
572637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	public void shouldReportWantedInvocationDiffersFromActual() {
582637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		wanted = buildIntArgMethod().arg(2222).toInvocationMatcher();
592637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		invocations = asList(buildIntArgMethod().arg(1111).toInvocation());
602637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
612637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expect(ArgumentsAreDifferent.class);
622637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
632637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("Argument(s) are different! Wanted:");
642637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("mock.intArgumentMethod(2222);");
652637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("Actual invocation has different arguments:");
662637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		exception.expectMessage("mock.intArgumentMethod(1111);");
672637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
682637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		MissingInvocationChecker.checkMissingInvocation(invocations, wanted);
692637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	}
702637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
712637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	private InvocationBuilder buildIntArgMethod() {
722637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		return new InvocationBuilder().mock(mock).method("intArgumentMethod").argTypes(int.class);
732637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	}
742637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
752637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	private InvocationBuilder buildSimpleMethod() {
762637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		return new InvocationBuilder().mock(mock).simpleMethod();
772637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	}
782637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
792637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	private InvocationBuilder buildDifferentMethod() {
802637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin		return new InvocationBuilder().mock(mock).differentMethod();
812637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin	}
822637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin}
83