Book Image

Mockito Cookbook

By : Marcin Grzejszczak
Book Image

Mockito Cookbook

By: Marcin Grzejszczak

Overview of this book

Table of Contents (17 chapters)
Mockito Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Verifying the method invocation count with atMost()


In this recipe, we will verify whether a method on a mock was executed, at most, a specified number of times.

Getting ready

As shown in the following code, our system under test is TaxUpdater (the same as that presented in the previous recipes):

public class TaxUpdater {

    static final double MEAN_TAX_FACTOR = 10.5;

    private final TaxService taxService;

    public TaxUpdater(TaxService taxService) {
        this.taxService = taxService;
    }

    public void updateTaxFactorFor(Person brother, Person sister) {
        taxService.updateMeanTaxFactor(brother, calculateMeanTaxFactor());
        taxService.updateMeanTaxFactor(sister, calculateMeanTaxFactor());
    }

    private double calculateMeanTaxFactor() {
        return MEAN_TAX_FACTOR;
    }

}

How to do it...

To verify whether the mock's method was invoked at most a given number of times, call Mockito.verify(mock, VerificationMode.atMost(count)).methodToVerify(...).

Let's check the...