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

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