Spying on real objects using Mockito Spy
In this tutorial we will explain more about Mockito Spies. We learn how to create a spy method, use the @Spy
annotation and finally learn how to stub a spy.
Dependencies
First we start by getting the necessary dependencies with Maven.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memorynotfound.test.mockito</groupId>
<artifactId>spy</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>MOCKITO - ${project.artifactId}</name>
<url>https://memorynotfound.com</url>
<properties>
<mockito.version>1.10.19</mockito.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Mockito spy method example
Here is a simple spy example. We can spy on a real object using the Mockito.spy()
method.
package com.memorynotfound.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class MockitoSpyMethodExampleTest {
@Test
public void testSpyOnList() {
List<String> messages = new ArrayList<String>();
List<String> spyMessages = spy(messages);
spyMessages.add("Mockito spy method");
spyMessages.add("Mockito spy method verify");
verify(spyMessages).add("Mockito spy method");
verify(spyMessages).add("Mockito spy method verify");
assertEquals(2, spyMessages.size());
}
}
Mockito @Spy Annotation example
An alternative to use the Mockito.spy()
method is to annotate the field with the @Spy
annotation.
package com.memorynotfound.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class MockitoSpyAnnotationExampleTest {
@Spy
List<String> messages = new ArrayList<String>();
@Test
public void testSpyOnList() {
messages.add("Mockito @Spy Annotation");
messages.add("Mockito @Spy verify");
verify(messages).add("Mockito @Spy Annotation");
verify(messages).add("Mockito @Spy verify");
assertEquals(2, messages.size());
}
}
Stubbing a spy
We can stub a spy using the Mockito.doReturn()
method.
package com.memorynotfound.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
@RunWith(MockitoJUnitRunner.class)
public class MockitoStubSpyExampleTest {
@Test
public void testSpyOnList() {
List<String> messages = new ArrayList<String>();
List<String> spyMessages = spy(messages);
String result = "mockito stubbing spy with value";
doReturn(result).when(spyMessages).get(13);
assertEquals(result, spyMessages.get(13));
}
}