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
About the Author
About the Reviewers


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. From the theoretical point of view, a spy is nothing but a partial mock, whose advantages and risks have been described in greater depth in the introduction to the previous chapter.

In general, you should use neither spies nor partial mocks in a well-designed code base. If you do use them, it most likely means that you are violating the S in the SOLID principles (described in more depth in the previous chapter). Let's have another look at that principle as a reminder.


(S) Single responsibility principle: a class should have only a single responsibility...