Mocking constructors (Medium)

At times, we come across a class that does a lot of work in its constructor itself. This can cause the constructor to become overly complicated and a road block for testing other classes.

In this recipe, we will learn how to mock and verify the invocation of a constructor.

Getting ready

A new requirement has come up while creating new employees. We want to send an e-mail message to welcome the new employees.

We will encapsulate the functionality of sending an e-mail in a class called WelcomeEmail. With this requirement in mind, let's look at how we can mock and verify constructors.

How to do it...

  1. Let's start off by creating a class called WelcomeEmail:
    /** * The class that is responsible to send the Welcome Email * to new ...

Get Instant Mock Testing with PowerMock now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.