Search in sources :

Example 6 with Invocation

use of org.mockito.invocation.Invocation 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 7 with Invocation

use of org.mockito.invocation.Invocation 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 8 with Invocation

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

the class NoMoreInteractions method verifyInOrder.

public void verifyInOrder(VerificationDataInOrder data) {
    List<Invocation> invocations = data.getAllInvocations();
    Invocation unverified = findFirstUnverifiedInOrder(data.getOrderingContext(), invocations);
    if (unverified != null) {
        throw noMoreInteractionsWantedInOrder(unverified);
    }
}
Also used : Invocation(org.mockito.invocation.Invocation)

Example 9 with Invocation

use of org.mockito.invocation.Invocation 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 10 with Invocation

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

the class MockitoDebuggerImpl method printInvocations.

/**
     * TODO: when MockitoDebugger is deleted, delete this implementation, too
     */
@Deprecated
public String printInvocations(Object... mocks) {
    String out = "";
    List<Invocation> invocations = AllInvocationsFinder.find(asList(mocks));
    out += line("********************************");
    out += line("*** Mockito interactions log ***");
    out += line("********************************");
    for (Invocation i : invocations) {
        out += line(i.toString());
        out += line(" invoked: " + i.getLocation());
        if (i.stubInfo() != null) {
            out += line(" stubbed: " + i.stubInfo().stubbedAt().toString());
        }
    }
    invocations = unusedStubsFinder.find(asList(mocks));
    if (invocations.isEmpty()) {
        return print(out);
    }
    out += line("********************************");
    out += line("***       Unused stubs       ***");
    out += line("********************************");
    for (Invocation i : invocations) {
        out += line(i.toString());
        out += line(" stubbed: " + i.getLocation());
    }
    return print(out);
}
Also used : Invocation(org.mockito.invocation.Invocation)

Aggregations

Invocation (org.mockito.invocation.Invocation)106 Test (org.junit.Test)71 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)28 MatchableInvocation (org.mockito.invocation.MatchableInvocation)19 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)11 Location (org.mockito.invocation.Location)7 InvocationsFinder.getLastLocation (org.mockito.internal.invocation.InvocationsFinder.getLastLocation)6 Returns (org.mockito.internal.stubbing.answers.Returns)6 VerificationDataInOrderImpl (org.mockito.internal.verification.api.VerificationDataInOrderImpl)6 Method (java.lang.reflect.Method)5 Discrepancy (org.mockito.internal.reporting.Discrepancy)4 MissingInvocationChecker.checkMissingInvocation (org.mockito.internal.verification.checkers.MissingInvocationChecker.checkMissingInvocation)4 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)3 InvocationsFinder.findFirstMatchingUnverifiedInvocation (org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation)3 InOrderContextImpl (org.mockito.internal.verification.InOrderContextImpl)3 IMethods (org.mockitousage.IMethods)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 LinkedList (java.util.LinkedList)2 TestCase.assertEquals (junit.framework.TestCase.assertEquals)2 MockingDetails (org.mockito.MockingDetails)2