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
About the Author
About the Reviewers

Injecting test doubles instead of beans with Guice

In this recipe, we will replace an existing bean with a test double using Guice's ( module configuration.

Getting ready

Let's assume that our system under test is the tax transferring system for a given person, as shown in the following code:

public class TaxTransferer {

    private final TaxService taxService;

    public TaxTransferer(TaxService taxService) {
        this.taxService = taxService;

    public boolean transferTaxFor(Person person) {
        if (person == null) {
            return false;
        return true;


Where the TaxService class is an interface that has an implementation called TaxWebService, which makes the web service call, as shown in the following code (for simplicity, we are only writing that we are performing such data exchange):

class TaxWebService implements TaxService {

    public void transferTaxFor...