Stubbing methods so that they return custom answers

In this recipe, we will stub a method that returns a value so that it returns a custom answer of our choice.

Getting ready

This recipe is the last that will reuse the example from the previous recipe, which is related to a class that calculates an average value of tax factors. The starting point is the AverageTaxFactorCalculator class and its collaborator is TaxFactorFetcher, which is the provider of those values. The latter class picks one of the tax factors from the database (we'll stub that method). We will test those two classes as a unit. For your convenience, even though it violates the don't repeat yourself (DRY) principle, we will see the classes as follows so that you don't have to scroll ...

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.