Search in sources :

Example 1 with Invocation

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

the class UnusedStubsFinder method find.

/**
     * Finds all unused stubs for given mocks
     *
     * @param mocks full list of mocks
     */
public List<Invocation> find(List<?> mocks) {
    List<Invocation> unused = new LinkedList<Invocation>();
    for (Object mock : mocks) {
        InternalMockHandler<Object> handler = MockUtil.getMockHandler(mock);
        List<StubbedInvocationMatcher> fromSingleMock = handler.getInvocationContainer().getStubbedInvocations();
        for (StubbedInvocationMatcher s : fromSingleMock) {
            if (!s.wasUsed()) {
                unused.add(s.getInvocation());
            }
        }
    }
    return unused;
}
Also used : Invocation(org.mockito.invocation.Invocation) StubbedInvocationMatcher(org.mockito.internal.stubbing.StubbedInvocationMatcher)

Example 2 with Invocation

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

the class DefaultStubbingLookupListener method potentialArgMismatches.

private static List<Invocation> potentialArgMismatches(Invocation invocation) {
    List<Invocation> matchingStubbings = new LinkedList<Invocation>();
    Collection<Stubbing> stubbings = mockingDetails(invocation.getMock()).getStubbings();
    for (Stubbing s : stubbings) {
        if (!s.wasUsed() && s.getInvocation().getMethod().getName().equals(invocation.getMethod().getName())) {
            matchingStubbings.add(s.getInvocation());
        }
    }
    return matchingStubbings;
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) LinkedList(java.util.LinkedList)

Example 3 with Invocation

use of org.mockito.invocation.Invocation 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 4 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 5 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)139 Test (org.junit.Test)95 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)36 MatchableInvocation (org.mockito.invocation.MatchableInvocation)21 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)13 Returns (org.mockito.internal.stubbing.answers.Returns)11 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)10 Location (org.mockito.invocation.Location)9 Method (java.lang.reflect.Method)7 LinkedList (java.util.LinkedList)7 MockitoException (org.mockito.exceptions.base.MockitoException)7 VerificationDataInOrderImpl (org.mockito.internal.verification.api.VerificationDataInOrderImpl)6 HashMap (java.util.HashMap)5 Map (java.util.Map)4 Producer (org.apache.pulsar.client.api.Producer)3 PulsarClient (org.apache.pulsar.client.api.PulsarClient)3 Function (org.apache.pulsar.functions.proto.Function)3 Request (org.apache.pulsar.functions.proto.Request)3 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)3 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)3