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 partial mocks of final classes with delegatesTo()


The previous recipe showed an example of working with code that is not very trivial to mock. The obstacles might be as follows:

  • There are cases where the classes to mock are final

  • The object to be mocked is an already proxied object (Mockito will have issues with dealing with those)

The main feature described in this recipe focuses on the process of delegation of a method execution from a method of the implemented interface to the instantiated class (as you can see, there is a catch – the class to be mocked needs to implement an interface). As a reminder, you should have very legitimate reasons to use a partial mock in your code. Otherwise, it most likely signifies that there is something wrong with the design of your code.

Getting ready

For this recipe, we will reuse the example from the previous recipe, but let's take another look at it. Our system under test for this recipe will be a TaxFactorProcessor class that interacts with a TaxService...