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

Creating mocks with different default answers with annotations


In the previous recipe, you have seen how to pass an implementation of the Answer interface to your mock to change its default behavior. In this recipe, we will focus on doing the same when creating mocks using annotations.

All versions of Mockito up until version 1.9.5 allow you to pass only elements of the Answers enum that delegate to answers present in the public Mockito API, as the arguments of the annotation. In the next Mockito release, there should be a possibility of passing a custom answer too, but until then it's not possible to do that.

Getting ready

In the following code, our system is a class that, based on the person's country, collects his Internal Revenue Service (IRS) address and formats it properly:

public class TaxFactorInformationProvider {

    private final TaxService taxService;

    public TaxFactorInformationProvider(TaxService taxService) {
        this.taxService = taxService;
    }

    public String...