Verifying the method invocation count with times()
In this recipe, we will verify whether a method on a mock was executed for exactly the given number of times.
Getting ready
For this recipe, our system under test will be TaxUpdater
, which calls TaxService
(let's assume that it is a web-service client) to update the mean tax factor for two people. Unfortunately, this system is old and can accept a single call at a time. For simplicity, the calculateMeanTaxFactor()
method, shown in the following code, returns a fixed value but in reality, there could be some complex logic:
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()); ...