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

Verifying that interactions stopped happening


In this recipe, we will verify that a specified method on a mock was executed and then any interactions stopped taking place.

Getting ready

For this recipe, our system under test will be a TaxTransferer class that will transfer tax for a non-null person. If the passed person value is null, then an error report is sent:

public class TaxTransferer {

    private final TaxService taxService;

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

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

}

How to do it...

To verify that the only method executed on a mock is the one provided by us, you have to call Mockito.verify(mock, VerificationMode.only()).methodToVerify(...).

Let's check the JUnit test that verifies whether the web service's method has been called at most...