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; ...

Get Mockito Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.