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

Creating mocks of enums with PowerMock


Believe it or not, in some legacy systems you can find solutions where the business logic is implemented inside an enum. What we will discuss here is how to mock and stub (you will learn about stubbing more in Chapter 4, Stubbing Behavior of Mocks) an enum using PowerMock (since it's impossible to do it in Mockito). The PowerMock library setup has been described in the previous recipe, so we'll skip it. I will, however, repeat that the best outcome of using the PowerMock library would be to use it as a means to refactor the code, and, at the end of the day, remove the PowerMock dependency from the system since it is no longer needed.

Getting ready

Let's assume that we have the following enum containing business logic:

public enum Country implements TaxRateCalculator {
    POLAND {
        @Override
        public double calculateTaxFactorFor(Person person) {
            return new PolishWebService().doLongOperation(person);
        }
    },
    OTHER ...