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

org.mockito.internal.verification.AtMost 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.verification;

import java.util.List;

import org.mockito.exceptions.Reporter;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.invocation.Invocation;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.invocation.InvocationMarker;
import org.mockito.internal.invocation.InvocationsFinder;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.internal.verification.api.VerificationMode;

public class AtMost implements VerificationMode {

    private final int maxNumberOfInvocations;
    private final InvocationMarker invocationMarker = new InvocationMarker();

    public AtMost(int maxNumberOfInvocations) {
        if (maxNumberOfInvocations < 0) {
            throw new MockitoException("Negative value is not allowed here");
        }
        this.maxNumberOfInvocations = maxNumberOfInvocations;
    }

    public void verify(VerificationData data) {
        List invocations = data.getAllInvocations();
        InvocationMatcher wanted = data.getWanted();
        
        InvocationsFinder finder = new InvocationsFinder();
        List found = finder.findInvocations(invocations, wanted);
        int foundSize = found.size();
        if (foundSize > maxNumberOfInvocations) {
            new Reporter().wantedAtMostX(maxNumberOfInvocations, foundSize);
        }
        
        invocationMarker.markVerified(found, wanted);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy