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

Get Mockito Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.