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

Capturing and asserting the argument


In this recipe, we will capture an argument passed to the mock's method to perform further verification.

Getting ready

For this recipe, our system under test will be a TaxTransferer class that will prepare the person to be sent through the web service by marking him a Polish citizen. Only if the person is not null, the transfer of tax will take place. Let's also assume that it is absolutely crucial for us to make sure that the person that we send via the web service contains very specific data:

public class TaxTransferer {

    static final String POLAND = "Poland";
    private final TaxService taxService;

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

    public void transferTaxFor(Person person) {
        if (person == null) {
            return;
        }
        taxService.transferTaxFor(makePersonPolish(person));
    }

    private Person makePersonPolish(Person person) {
        return new Person(person...