Book Image

MOCKITO COOKBOOK

By : Grzejszczak
Book Image

MOCKITO COOKBOOK

By: Grzejszczak

Overview of this book

This is a focused guide with lots of practical recipes with presentations of business issues and presentation of the whole test of the system. This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on. If you are a software developer with no testing experience (especially with Mockito) and you want to start using Mockito in the most efficient way then this book is for you. This book assumes that you have a good knowledge level and understanding of Java-based unit testing frameworks.
Table of Contents (12 chapters)
11
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...