/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.verification.checkers;
import static java.util.Arrays.*;
import java.util.LinkedList;
import org.junit.Before;
import org.junit.Test;
import org.mockito.exceptions.Reporter;
import org.mockito.exceptions.verification.VerificationInOrderFailure;
import org.mockito.internal.invocation.InvocationImpl;
import org.mockito.internal.invocation.InvocationBuilder;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.verification.InOrderContextImpl;
import org.mockito.internal.verification.api.InOrderContext;
import org.mockito.invocation.Invocation;
import org.mockitoutil.TestBase;
public class NumberOfInvocationsInOrderCheckerTest extends TestBase {
private NumberOfInvocationsInOrderChecker checker;
private Reporter reporter;
private InvocationMatcher wanted;
private LinkedList<Invocation> invocations;
private InvocationsFinderStub finderStub;
private InOrderContext context = new InOrderContextImpl();
@Before
public void setup() {
reporter = new Reporter();
finderStub = new InvocationsFinderStub();
checker = new NumberOfInvocationsInOrderChecker(finderStub, reporter);
wanted = new InvocationBuilder().toInvocationMatcher();
invocations = new LinkedList<Invocation>(asList(new InvocationBuilder().toInvocation()));
}
@Test
public void shouldPassIfWantedIsZeroAndMatchingChunkIsEmpty() throws Exception {
assertTrue(finderStub.validMatchingChunkToReturn.isEmpty());
checker.check(invocations, wanted, 0, context);
}
@Test
public void shouldPassIfChunkMatches() throws Exception {
finderStub.validMatchingChunkToReturn.add(wanted.getInvocation());
checker.check(invocations, wanted, 1, context);
}
@Test
public void shouldReportTooLittleInvocations() throws Exception {
Invocation first = new InvocationBuilder().toInvocation();
Invocation second = new InvocationBuilder().toInvocation();
finderStub.validMatchingChunkToReturn.addAll(asList(first, second));
try {
checker.check(invocations, wanted, 4, context);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("Wanted 4 times", e.getMessage());
assertContains("But was 2 times", e.getMessage());
}
}
@Test
public void shouldReportTooManyInvocations() throws Exception {
Invocation first = new InvocationBuilder().toInvocation();
Invocation second = new InvocationBuilder().toInvocation();
finderStub.validMatchingChunkToReturn.addAll(asList(first, second));
try {
checker.check(invocations, wanted, 1, context);
fail();
} catch (VerificationInOrderFailure e) {
assertContains("Wanted 1 time", e.getMessage());
assertContains("But was 2 times", e.getMessage());
}
}
@Test
public void shouldMarkAsVerifiedInOrder() throws Exception {
Invocation invocation = new InvocationBuilder().toInvocation();
assertFalse(context.isVerified(invocation));
finderStub.validMatchingChunkToReturn.addAll(asList(invocation));
checker.check(invocations, wanted, 1, context);
assertTrue(context.isVerified(invocation));
}
}