Search in sources :

Example 11 with MatchableInvocation

use of org.mockito.invocation.MatchableInvocation in project mockito by mockito.

the class NumberOfInvocationsChecker method checkNumberOfInvocations.

public static void checkNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount) {
    List<Invocation> actualInvocations = findInvocations(invocations, wanted);
    int actualCount = actualInvocations.size();
    if (wantedCount > actualCount) {
        Location lastInvocation = getLastLocation(actualInvocations);
        throw tooLittleActualInvocations(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation);
    }
    if (wantedCount == 0 && actualCount > 0) {
        Location firstUndesired = actualInvocations.get(wantedCount).getLocation();
        throw neverWantedButInvoked(wanted, firstUndesired);
    }
    if (wantedCount < actualCount) {
        Location firstUndesired = actualInvocations.get(wantedCount).getLocation();
        throw tooManyActualInvocations(wantedCount, actualCount, wanted, firstUndesired);
    }
    markVerified(actualInvocations, wanted);
}
Also used : Discrepancy(org.mockito.internal.reporting.Discrepancy) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationsFinder.findFirstMatchingUnverifiedInvocation(org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation) Location(org.mockito.invocation.Location) InvocationsFinder.getLastLocation(org.mockito.internal.invocation.InvocationsFinder.getLastLocation)

Example 12 with MatchableInvocation

use of org.mockito.invocation.MatchableInvocation in project mockito by mockito.

the class NumberOfInvocationsChecker method checkNumberOfInvocationsNonGreedy.

public static void checkNumberOfInvocationsNonGreedy(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount, InOrderContext context) {
    int actualCount = 0;
    Location lastLocation = null;
    while (actualCount < wantedCount) {
        Invocation next = findFirstMatchingUnverifiedInvocation(invocations, wanted, context);
        if (next == null) {
            throw tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastLocation);
        }
        markVerified(next, wanted);
        context.markVerified(next);
        lastLocation = next.getLocation();
        actualCount++;
    }
}
Also used : Discrepancy(org.mockito.internal.reporting.Discrepancy) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationsFinder.findFirstMatchingUnverifiedInvocation(org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation) Location(org.mockito.invocation.Location) InvocationsFinder.getLastLocation(org.mockito.internal.invocation.InvocationsFinder.getLastLocation)

Example 13 with MatchableInvocation

use of org.mockito.invocation.MatchableInvocation in project mockito by mockito.

the class NumberOfInvocationsChecker method checkNumberOfInvocations.

public static void checkNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount, InOrderContext context) {
    List<Invocation> chunk = findMatchingChunk(invocations, wanted, wantedCount, context);
    int actualCount = chunk.size();
    if (wantedCount > actualCount) {
        Location lastInvocation = getLastLocation(chunk);
        throw tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation);
    }
    if (wantedCount < actualCount) {
        Location firstUndesired = chunk.get(wantedCount).getLocation();
        throw tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, firstUndesired);
    }
    markVerifiedInOrder(chunk, wanted, context);
}
Also used : Discrepancy(org.mockito.internal.reporting.Discrepancy) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationsFinder.findFirstMatchingUnverifiedInvocation(org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation) Location(org.mockito.invocation.Location) InvocationsFinder.getLastLocation(org.mockito.internal.invocation.InvocationsFinder.getLastLocation)

Example 14 with MatchableInvocation

use of org.mockito.invocation.MatchableInvocation in project mockito by mockito.

the class InvocationMarkerTest method shouldCaptureArguments.

@Test
public void shouldCaptureArguments() {
    //given
    Invocation i = new InvocationBuilder().toInvocation();
    final AtomicReference<Invocation> box = new AtomicReference<Invocation>();
    MatchableInvocation c = new InvocationMatcher(i) {

        public void captureArgumentsFrom(Invocation i) {
            box.set(i);
        }
    };
    //when
    InvocationMarker.markVerified(Arrays.asList(i), c);
    //then
    assertEquals(i, box.get());
}
Also used : MatchableInvocation(org.mockito.invocation.MatchableInvocation) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

Invocation (org.mockito.invocation.Invocation)14 MatchableInvocation (org.mockito.invocation.MatchableInvocation)14 InvocationsFinder.getLastLocation (org.mockito.internal.invocation.InvocationsFinder.getLastLocation)6 Location (org.mockito.invocation.Location)6 Discrepancy (org.mockito.internal.reporting.Discrepancy)4 InvocationsFinder.findFirstMatchingUnverifiedInvocation (org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation)3 MissingInvocationChecker.checkMissingInvocation (org.mockito.internal.verification.checkers.MissingInvocationChecker.checkMissingInvocation)3 InvocationsFinder.findSimilarInvocation (org.mockito.internal.invocation.InvocationsFinder.findSimilarInvocation)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 SmartPrinter (org.mockito.internal.reporting.SmartPrinter)1