Chapter 3. Creating Spies and Partial Mocks

In this chapter, we will cover the following recipes:

  • Creating spies in code
  • Creating spies with custom configuration
  • Creating spies using annotations
  • Creating partial mocks
  • Creating partial mocks of final classes with delegatesTo()
  • Creating spies of final classes with PowerMock

Introduction

Before going into the details regarding how to create a spy, let's first consider what a spy really is. It's an object that may have predefined answers to its method executions, whereas by default it calls the real implementation. It also records the method calls for further verification. So how does it differ from any other test double? Well, apart from the fact that you can stub its methods, you can also verify its behavior. ...

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.