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

Stubbing void methods so that they return custom answers


In this recipe, we will stub a void method. It doesn't return a value, so it returns a custom answer.

Getting ready

In this recipe, we'll reuse the example from the previous recipes. A quick reminder again – we have a system under test that consists of two objects: a PersonDataUpdator class that delegates work to TaxFactorService. The output of the system is a calculation of a mean value of tax factors (we have a fixed value for that). The person's data then gets updated via a web service. In this scenario, we will verify how our system works when an exception related to connectivity issues occurs. Have a look at the following code:

public class PersonDataUpdator {

  private final TaxFactorService taxFactorService;

  public PersonDataUpdator(TaxFactorService taxFactorService) {
    this.taxFactorService = taxFactorService;
  }

  public boolean processTaxDataFor(Person person) {
    try {
      double meanTaxFactor = taxFactorService...