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 using Spring's XML configuration

In the following recipe, we will replace an existing bean with a test double using Spring's XML 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;


As shown in the previous example, TaxService is a class that will perform a web service call. For readability purposes, we are simulating that we have such data exchanged as follows:

class TaxService {

    public void transferTaxFor(Person person) {
        System.out.printf("Calling external web service for person with name ...