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

Refactoring the classes that use the class casts


In this recipe, we will focus on fixing the wrong design of an application that performs logic based on the types of passed objects. The instanceof operator distinguishes the types, and then class casting takes place.

Getting ready

As we did in the previous recipe, let's assume that our system under test is a system that generates a new identity for a given person who can have a name, an age, and siblings, and who sends a JSON message over a web service. Note that the following snippet presents a poorly designed class (all the test examples are written for JUnit; please refer to Chapter 1, Getting Started with Mockito, for the TestNG configuration and Chapter 7, Verifying Behavior with Object Matchers, for the AssertJ configuration and the BDDAssertions static imports):

public class AwefullyCastingNewPersonGenerator {

    private final IdentityCreator identityCreator;

    public AwefullyCastingNewPersonGenerator(IdentityCreator identityCreator...