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

Stubbing static methods with PowerMock


The current recipe will be about stubbing a static method in order to properly verify the behavior of the system under test. Unfortunately, Mockito can't stub static methods, and that's why we will use PowerMock to do that.

I'd like to yet again remind you that it absolutely isn't good practice to use PowerMock in your well-written code. If you follow all of the SOLID principles (please refer to Chapter 2, Creating Mocks, for the explanation of each of these principles), then you should not resort to stubbing static methods. PowerMock can come in hand when dealing with legacy code or stubbing third-party libraries (you can check Chapter 8, Refactoring with Mockito, to see how to use PowerMock to refactor the badly written code).

Getting ready

To use PowerMock, you have to add it to your classpath. Please check the Creating mocks of final classes with PowerMock recipe in Chapter 2, Creating Mocks, for more details on how to add PowerMock to your project...