Search in sources :

Example 6 with Stubbing

use of org.mockito.stubbing.Stubbing in project mockito by mockito.

the class DefaultMockingDetails method getStubbings.

@Override
public Collection<Stubbing> getStubbings() {
    List<? extends Stubbing> stubbings = mockHandler().getInvocationContainer().getStubbedInvocations();
    TreeSet<Stubbing> out = new TreeSet<Stubbing>(new StubbingComparator());
    out.addAll(stubbings);
    return out;
}
Also used : StubbingComparator(org.mockito.internal.stubbing.StubbingComparator) Stubbing(org.mockito.stubbing.Stubbing) TreeSet(java.util.TreeSet)

Example 7 with Stubbing

use of org.mockito.stubbing.Stubbing in project mockito by mockito.

the class InvocationsPrinter method printInvocations.

public String printInvocations(Object mock) {
    Collection<Invocation> invocations = Mockito.mockingDetails(mock).getInvocations();
    Collection<Stubbing> stubbings = Mockito.mockingDetails(mock).getStubbings();
    if (invocations.isEmpty() && stubbings.isEmpty()) {
        return "No interactions and stubbings found for mock: " + mock;
    }
    StringBuilder sb = new StringBuilder();
    int x = 1;
    for (Invocation i : invocations) {
        if (x == 1) {
            sb.append("[Mockito] Interactions of: ").append(mock).append("\n");
        }
        sb.append(" ").append(x++).append(". ").append(i).append("\n");
        sb.append("  ").append(i.getLocation()).append("\n");
        if (i.stubInfo() != null) {
            sb.append("   - stubbed ").append(i.stubInfo().stubbedAt()).append("\n");
        }
    }
    LinkedList<Stubbing> unused = ListUtil.filter(stubbings, new ListUtil.Filter<Stubbing>() {

        @Override
        public boolean isOut(Stubbing s) {
            return s.wasUsed();
        }
    });
    if (unused.isEmpty()) {
        return sb.toString();
    }
    sb.append("[Mockito] Unused stubbings of: ").append(mock).append("\n");
    x = 1;
    for (Stubbing s : stubbings) {
        sb.append(" ").append(x++).append(". ").append(s.getInvocation()).append("\n");
        sb.append("  - stubbed ").append(s.getInvocation().getLocation()).append("\n");
    }
    return sb.toString();
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) Invocation(org.mockito.invocation.Invocation) ListUtil(org.mockito.internal.util.collections.ListUtil)

Example 8 with Stubbing

use of org.mockito.stubbing.Stubbing in project mockito by mockito.

the class UnusedStubbings method format.

void format(String testName, MockitoLogger logger) {
    if (unused.isEmpty()) {
        return;
    }
    StubbingHint hint = new StubbingHint(testName);
    int x = 1;
    for (Stubbing candidate : unused) {
        if (!candidate.wasUsed()) {
            hint.appendLine(x++, ". Unused ", candidate.getInvocation().getLocation());
        }
    }
    logger.log(hint.toString());
}
Also used : Stubbing(org.mockito.stubbing.Stubbing)

Aggregations

Stubbing (org.mockito.stubbing.Stubbing)8 Invocation (org.mockito.invocation.Invocation)4 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 StubbingComparator (org.mockito.internal.stubbing.StubbingComparator)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)1 DefaultMockingDetails (org.mockito.internal.util.DefaultMockingDetails)1 ListUtil (org.mockito.internal.util.collections.ListUtil)1 Location (org.mockito.invocation.Location)1 MatchableInvocation (org.mockito.invocation.MatchableInvocation)1 MockHandlerAdaptor (org.powermock.api.mockito.invocation.MockHandlerAdaptor)1 MockitoMethodInvocationControl (org.powermock.api.mockito.invocation.MockitoMethodInvocationControl)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1