MissingInvocationCheckerTest.java revision 2637d96c202372854a7c71466ddcc6e90fc4fc53
1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6package org.mockito.internal.verification.checkers; 7 8import static java.util.Arrays.asList; 9 10import java.util.List; 11import org.junit.Rule; 12import org.junit.Test; 13import org.junit.rules.ExpectedException; 14import org.mockito.Mock; 15import org.mockito.exceptions.verification.WantedButNotInvoked; 16import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; 17import org.mockito.internal.invocation.InvocationBuilder; 18import org.mockito.internal.invocation.InvocationMatcher; 19import org.mockito.invocation.Invocation; 20import org.mockitousage.IMethods; 21import org.mockitoutil.TestBase; 22 23public 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 35 public void shouldPassBecauseActualInvocationFound() { 36 wanted = buildSimpleMethod().toInvocationMatcher(); 37 invocations = asList(buildSimpleMethod().toInvocation()); 38 39 MissingInvocationChecker.checkMissingInvocation(invocations, wanted); 40 } 41 42 @Test 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 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 71 private InvocationBuilder buildIntArgMethod() { 72 return new InvocationBuilder().mock(mock).method("intArgumentMethod").argTypes(int.class); 73 } 74 75 private InvocationBuilder buildSimpleMethod() { 76 return new InvocationBuilder().mock(mock).simpleMethod(); 77 } 78 79 private InvocationBuilder buildDifferentMethod() { 80 return new InvocationBuilder().mock(mock).differentMethod(); 81 } 82} 83