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 spies of final classes with PowerMock


Before going into the details of this recipe, if you haven't already done so, please read the Creating mocks of final classes with PowerMock recipe of Chapter 2, Creating Mocks. PowerMock is a powerful (thus dangerous) tool, that in the hands of an inexperienced developer, can lead to the creation of really bad test and production code.

Why would you want to use PowerMock? Mockito can't create mocks for classes that are final. The same problem exists when trying to create spies. If you have a properly written test-driven code, you shouldn't have the need to use either spies or partial mocks, nor have PowerMock in your project. If you need to use PowerMock to create spies of final classes, do it only as a last resort in order to refactor your code, and at the end remove PowerMock dependencies and only use Mockito. Refer to Chapter 8, Refactoring with Mockito, for examples of how to use PowerMock as a mean to refactor your code (and at the end...