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

org.mockito.internal.verification.Calls 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 org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.internal.verification.api.VerificationDataInOrder;
import org.mockito.internal.verification.api.VerificationInOrderMode;
import org.mockito.internal.verification.checkers.*;
import org.mockito.invocation.Invocation;
import org.mockito.verification.VerificationMode;

import java.util.List;

public class Calls implements VerificationMode, VerificationInOrderMode {

    final int wantedCount;

    public Calls(int wantedNumberOfInvocations) {
        if( wantedNumberOfInvocations <= 0 ) {
            throw new MockitoException( "Negative and zero values are not allowed here" );
        }
        this.wantedCount = wantedNumberOfInvocations;
    }

    public void verify(VerificationData data) {
        throw new MockitoException( "calls is only intended to work with InOrder" );
    }

    public void verifyInOrder(VerificationDataInOrder data) {
        List allInvocations = data.getAllInvocations();
        InvocationMatcher wanted = data.getWanted();
        
        MissingInvocationInOrderChecker missingInvocation = new MissingInvocationInOrderChecker();
        missingInvocation.check( allInvocations, wanted, this, data.getOrderingContext());
        NonGreedyNumberOfInvocationsInOrderChecker numberOfCalls = new NonGreedyNumberOfInvocationsInOrderChecker();
        numberOfCalls.check( allInvocations, wanted, wantedCount, data.getOrderingContext());
    }    
    
    @Override
    public String toString() {
        return "Wanted invocations count (non-greedy): " + wantedCount;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy