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

com.fitbur.mockito.internal.verification.checkers.NumberOfInvocationsChecker Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */

package com.fitbur.mockito.internal.verification.checkers;

import static com.fitbur.mockito.exceptions.Reporter.neverWantedButInvoked;
import static com.fitbur.mockito.exceptions.Reporter.tooLittleActualInvocations;
import static com.fitbur.mockito.exceptions.Reporter.tooManyActualInvocations;
import static com.fitbur.mockito.internal.invocation.InvocationMarker.markVerified;

import java.util.List;

import com.fitbur.mockito.internal.invocation.InvocationMatcher;
import com.fitbur.mockito.internal.invocation.InvocationsFinder;
import com.fitbur.mockito.internal.reporting.Discrepancy;
import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.invocation.Location;

public class NumberOfInvocationsChecker {
    
    private final InvocationsFinder finder=new InvocationsFinder();
    
    public void check(List invocations, InvocationMatcher wanted, int wantedCount) {
        List actualInvocations = finder.findInvocations(invocations, wanted);
        
        int actualCount = actualInvocations.size();
        if (wantedCount > actualCount) {
            Location lastInvocation = finder.getLastLocation(actualInvocations);
            throw tooLittleActualInvocations(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation);
        } 
        if (wantedCount == 0 && actualCount > 0) {
            Location firstUndesired = actualInvocations.get(wantedCount).getLocation();
            throw neverWantedButInvoked(wanted, firstUndesired); 
        } 
        if (wantedCount < actualCount) {
            Location firstUndesired = actualInvocations.get(wantedCount).getLocation();
            throw tooManyActualInvocations(wantedCount, actualCount, wanted, firstUndesired);
        }
        
        markVerified(actualInvocations, wanted);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy