O'Reilly logo

Instant Mock Testing with PowerMock by Deep Shah

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required