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

Mockito versus EasyMock

In this recipe, we will write a simple test using EasyMock that verifies the behavior of the system under test when an exception is thrown.

Getting ready

In order to profit from EasyMock, you need to add it to your classpath. This is the configuration for Gradle:

testCompile 'org.easymock:easymock:3.2'

The following is how you add the EasyMock dependency in Maven:


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 (taxService.hasAlreadyTransferredTax(person)) {