Mockito Spy example


In this lesson on Spy in Mockito, we will see how Spies differ from Mocks and how are these used.

Adding to classpath, using Maven

The fastest way to add Mockito to your project is using Maven dependency.

This dependency is simple enough and does not bring any additional or redundant libraries. See here for latest versions of the library.

Using Mockito Annotations

Now, to start using Mockito Annotations, we must enable them beforehand in our Test class. There are 2 ways this can be done. By annotating with MockitoJUnitRunner:

Or, this can also be done manually programmatically in a @Before annotated method, like:

Spies in Mockito

When Mocks are created in Mockito, they are done from the class of a Type, not from the actual instance. This is not the case with Spies.

Spy works just like real instances, it will behave in the same way as a real instance does, just that a Spy is instrumented in a way that all interactions with it can be tracked, like a method call or value initialization. Let’s see what tracking of interaction means with an example:


  • A Map Spy was made using static spy(…) method
  • Then, a key-value pair was added to it
  • After the element was added, we verified its interaction as element addition was done
  • Note that a real instance of Map was made and we even verified it using assertEquals to check its size, it was actually 1.

Let’s do the same example again, using @Spy annotation:

Not much difference in that. Do remember that as we show in the beginning of this lesson, we need to activate Mockito annotations using one of the methods shown in the beginning.

Stubbing a Spy

Now, let’s see how we can stub a Spy. Using stubbing, we can override the behavior of a method and modify its return values, just like what we do in Mocks.

In our example, we will override the behavior of size() method of Map interface:

Another example

Let’s say you have an Employee class. This employee class has an object of Address class.
Method under test:

We are going to test Employee class’s getEmployeeDetails method. Below is the method we are going to test.

Now in above method, let’s suppose Address’s getAddressDetails() method do a database call and fetch address details.You don’t want to do DB connectin in your test, so you can simply mock this method and test functionality of getEmployeeDetails().
Create a class named “”

Create another class named “”

Create a test class named “EmployeeDetailsTest”

As you can see here, we have used @Spy with Address object.

Above lines mocks getAddressDetails() method which is database operation which we have successfully avoided using Mockito.

Mock vs Spy

When Mockito creates a mock – it does so from the Class of a Type, not from an actual instance. On the other hand, a spy will be an original instance. It will still behave in the same way as the normal instance – the only difference is that it will also be instrumented to track all the interactions with it. All the methods of a spy are real unless stubbed.


Add Comment