O'Reilly logo

Mockito Cookbook by Marcin Grzejszczak

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

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

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