Stubbing methods so that they call real methods

In this recipe, we will stub a method that returns a value so that it calls a real method. This way, we will construct a partial mock (to read more about partial mocking, please refer to Chapter 2, Creating Mocks).

Getting ready

For this recipe, our system under test will be MeanTaxFactorCalculator, which calls TaxFactorFetcher twice to get a tax factor for the given person and then calculates a mean value for those two results as follows:

public class MeanTaxFactorCalculator { private final TaxFactorFetcher taxFactorFetcher; public MeanTaxFactorCalculator(TaxFactorFetcher taxFactorFetcher) { this.taxFactorFetcher = taxFactorFetcher; } public double calculateMeanTaxFactorFor(Person person) { double ...

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.