Thursday, June 16, 2016

Mocking of enum singleton class with PowerMock and Mockito

The earlier post Mocking with PowerMock and Mockito explains how to use the PowerMock for mocking of static classes. Another method of mocking with the PowerMock is the Whitebox class. It provides various utilities for accessing internals of a class. A singleton class which is implemented with enum singleton pattern, may be easily mocked with the Whitebox.setInternalState.

In the example below a singleton DAOFactory contains several DAO services as enum values. Each DAO service supports DB access to a particular DB entity.
The factory class:

No comments
:

About the author

I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.