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


Although these situations should not take place in a well-written and test-driven system, there are cases in which it is necessary to mock some legacy code or third-party libraries that are impossible to be mocked only by means of Mockito. In this recipe, we will see how to deal with those abnormal situations using the PowerMock library. Remember, however, that this tool is extremely powerful and the very need to use it suggests that something may really be wrong with your code. The best outcome of using this library would be to use it as means to refactor the bad code and, at the end of the day, remove the PowerMock dependency from the system since it is no longer needed.

Getting ready

In order to use PowerMock with Mockito, you need to include the following library in your classpath. If you are using a dependency management system such as Gradle or Maven, you can add it to the code as follows:

The dependency definition for Gradle is as follows...