All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.internal.debugging.WarningsPrinterImpl Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.internal.debugging;

import static org.mockito.internal.util.StringJoiner.*;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.mockito.internal.invocation.Invocation;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.util.MockitoLogger;

public class WarningsPrinterImpl {

    private final List unusedStubs;
    private final List unstubbedInvocations;
    private final boolean warnAboutUnstubbed;

    public WarningsPrinterImpl(List unusedStubs, List unstubbedInvocations) {
        this(unusedStubs, unstubbedInvocations, false);
    }

    public WarningsPrinterImpl(List unusedStubs, List unstubbedInvocations, boolean warnAboutUnstubbed) {
        this.warnAboutUnstubbed = warnAboutUnstubbed;
        this.unusedStubs = new LinkedList(unusedStubs);
        this.unstubbedInvocations = new LinkedList(unstubbedInvocations);
    }
    
    public void print(MockitoLogger logger) {
        Iterator unusedIterator = unusedStubs.iterator();
        while(unusedIterator.hasNext()) {
            Invocation unused = unusedIterator.next();
            Iterator unstubbedIterator = unstubbedInvocations.iterator();
            while(unstubbedIterator.hasNext()) {
                InvocationMatcher unstubbed = unstubbedIterator.next();
                if(unstubbed.hasSimilarMethod(unused)) { 
                    logger.log(stubbedMethodCalledWithDifferentArguments(unused, unstubbed));
                    unusedIterator.remove();
                    unstubbedIterator.remove();
                } 
            }
        }
        
        for (Invocation i : unusedStubs) {
            logger.log(thisStubWasNotUsed(i));
        }

        if (warnAboutUnstubbed) {
            for (InvocationMatcher i1 : unstubbedInvocations) {
                logger.log(thisMethodWasNotStubbed(i1));
            }
        }
    }

    private String thisStubWasNotUsed(Invocation i) {
        return "This stubbing was never used " + i.getLocation() + "\n";
    }

    private String thisMethodWasNotStubbed(InvocationMatcher i) {
        return join(
            "[Mockito hint] This method was not stubbed:",
            i,
            i.getInvocation().getLocation(),
            "");
    }

    private String stubbedMethodCalledWithDifferentArguments(Invocation unused, InvocationMatcher unstubbed) {
        return join(
                " *** Verbose stubbing warnings from Mockito *** ",
                "stubbed here " + unused.getLocation(),
                "BUT called with different arguments here " + unstubbed.getInvocation().getLocation(),
                "");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy