Search in sources :

Example 1 with MatchableInvocation

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

the class AtLeastXNumberOfInvocationsChecker method checkAtLeastNumberOfInvocations.

public static void checkAtLeastNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount) {
    List<Invocation> actualInvocations = findInvocations(invocations, wanted);
    int actualCount = actualInvocations.size();
    if (wantedCount > actualCount) {
        Location lastLocation = getLastLocation(actualInvocations);
        throw tooLittleActualInvocations(new AtLeastDiscrepancy(wantedCount, actualCount), wanted, lastLocation);
    }
    markVerified(actualInvocations, wanted);
}
Also used : Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationsFinder.getLastLocation(org.mockito.internal.invocation.InvocationsFinder.getLastLocation) Location(org.mockito.invocation.Location)

Example 2 with MatchableInvocation

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

the class MissingInvocationChecker method checkMissingInvocation.

public static void checkMissingInvocation(List<Invocation> invocations, MatchableInvocation wanted) {
    List<Invocation> actualInvocations = findInvocations(invocations, wanted);
    if (!actualInvocations.isEmpty()) {
        return;
    }
    Invocation similar = findSimilarInvocation(invocations, wanted);
    if (similar == null) {
        throw wantedButNotInvoked(wanted, invocations);
    }
    Integer[] indexesOfSuspiciousArgs = getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(), similar.getArguments());
    SmartPrinter smartPrinter = new SmartPrinter(wanted, similar, indexesOfSuspiciousArgs);
    throw argumentsAreDifferent(smartPrinter.getWanted(), smartPrinter.getActual(), similar.getLocation());
}
Also used : Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationsFinder.findSimilarInvocation(org.mockito.internal.invocation.InvocationsFinder.findSimilarInvocation) SmartPrinter(org.mockito.internal.reporting.SmartPrinter)

Example 3 with MatchableInvocation

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

the class NumberOfInvocationsInOrderChecker method check.

public void check(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.getLastLocation(org.mockito.internal.invocation.InvocationsFinder.getLastLocation) Location(org.mockito.invocation.Location)

Example 4 with MatchableInvocation

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

the class Only method verify.

@SuppressWarnings("unchecked")
public void verify(VerificationData data) {
    MatchableInvocation target = data.getTarget();
    List<Invocation> invocations = data.getAllInvocations();
    List<Invocation> chunk = findInvocations(invocations, target);
    if (invocations.size() != 1 && chunk.size() > 0) {
        Invocation unverified = findFirstUnverified(invocations);
        throw noMoreInteractionsWanted(unverified, (List) invocations);
    }
    if (invocations.size() != 1 || chunk.size() == 0) {
        throw wantedButNotInvoked(target);
    }
    markVerified(chunk.get(0), target);
}
Also used : MatchableInvocation(org.mockito.invocation.MatchableInvocation) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation)

Example 5 with MatchableInvocation

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

the class AtMost method verify.

public void verify(VerificationData data) {
    List<Invocation> invocations = data.getAllInvocations();
    MatchableInvocation wanted = data.getTarget();
    List<Invocation> found = findInvocations(invocations, wanted);
    int foundSize = found.size();
    if (foundSize > maxNumberOfInvocations) {
        throw wantedAtMostX(maxNumberOfInvocations, foundSize);
    }
    removeAlreadyVerified(found);
    markVerified(found, wanted);
}
Also used : MatchableInvocation(org.mockito.invocation.MatchableInvocation) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation)

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