Mockito - Spy vs Mock
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Mockito is a popular Java-based mocking framework used for unit testing by creating mock objects. Understanding the distinction between different Mockito annotations like @Mock and @Spy is crucial for effective testing and ensuring clean code. In this article, we will delve into the differences, use cases, and examples associated with these annotations.
Overview of Mockito
Mockito simplifies the testing of Java applications by allowing developers to create mock objects for the components that are out of scope for the unit test. Using mock objects can drastically reduce the complexity of the test setup, as external dependencies can be easily managed or altered to suit the needs of a test case.
@Mock vs @Spy
Both @Mock and @Spy serve distinct purposes within the Mockito framework. Here is a technical differentiation between the two:
- @Mock: This annotation is used to create a mock object of a class or interface. When the object is mocked, all of its methods are stubbed to return default values. You can specify behavior using
Mockito.when()and configure the mock object as desired. - @Spy: This annotation indicates that the object is a partial mock or spy. Unlike
@Mock, the object retains its original behavior unless a method is explicitly stubbed.@Spyallows calling real methods unless explicitly mocked.
Key Differences
| Feature/Aspect | @Mock | @Spy |
| Object behavior | Mocks all methods within the object to return default values unless specified manually. | Calls real methods unless they are specifically stubbed. |
| Use case | Ideal when the entire behavior of the dependency needs to be abstracted or simulated. | Useful when testing particular methods while relying on the actual behavior of others. |
| Interaction verification | Focuses more on setting expected interactions between the test subject and its dependencies. | Important for verifying real interactions and assessing partial behavior changes. |
| Implementation Nature | Pure mock object without any underlying instance of the class. | Creates a spy object that is a wrapper around the actual object instance. |
Technical Examples
Using @Mock
Using @Spy
Advanced Features: Mixing @Mock and @Spy
In complex scenarios, you might decide to combine @Mock and @Spy. For instance, you might mock a data repository but still spy on the service to verify certain aspects of its behavior without altering its business logic significantly.
Here, the exampleRepository is fully mocked, while the exampleServiceWithSpy uses the real service's logic but with controlled behavior of specific methods, if required.
Subtopics
Partial Mocks vs Full Mocks
Partial mocks, possible with @Spy, may be necessary when you want to test the functionality of a unit with partial replacement of its methods. This bypasses the limitation of traditional full mocks where you mock every method behavior.
Exception Testing
With both @Mock and @Spy, developers can test how their units handle exceptions. This is crucial for ensuring reliability and robustness in error handling.
Conclusion
Choosing between @Mock and @Spy primarily depends on the test goals. While @Mock is widely used for simulating and abstracting away external interactions, @Spy provides a blend of real and mock behavior that is advantageous in scenarios requiring partial method stubbing.
Understanding when and how to use each effectively can significantly contribute to the robustness of unit testing suites, making the development cycle both efficient and reliable.
Related reading
- Mockito doAnswer Vs thenReturn
- Mockito error in Spring Boot tests after migrating to JDK 11
- Mockito How to mock and assert a thrown exception?
- Mockito how to verify method was called on an object created within a method?
- Mockito Inject real objects into private @Autowired fields
- Mockito InvalidUseOfMatchersException
- Mockito is currently self-attaching to enable the inline-mock-maker. This will no longer work in future releases of the JDK
- Mockito List Matchers with generics

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.