Recall & Review
beginner
What is a Spy object in testing?
A Spy object is a special test double that wraps a real object to record how it is used, allowing verification of method calls while still executing the original methods.
Click to reveal answer
intermediate
How does a Spy differ from a Mock?
A Spy calls the real methods and records interactions, while a Mock replaces the real object and only simulates behavior without calling real methods.
Click to reveal answer
beginner
Show a simple example of creating a Spy in JUnit with Mockito.
Example:<br>
List<String> list = new ArrayList<>();
List<String> spyList = Mockito.spy(list);
spyList.add("test");
Mockito.verify(spyList).add("test");Click to reveal answer
intermediate
Why use a Spy object instead of a real object in tests?
Using a Spy lets you check how the object is used (like which methods were called) without losing the real behavior, helping to verify interactions and side effects.
Click to reveal answer
advanced
What is a common pitfall when using Spy objects?
A common pitfall is that Spies call real methods, which can cause unwanted side effects or errors if the real methods depend on external systems or state.
Click to reveal answer
What does a Spy object do in a test?
✗ Incorrect
A Spy calls the real methods of the object and records how they are used for verification.
Which library is commonly used with JUnit to create Spy objects?
✗ Incorrect
Mockito is a popular mocking framework that supports creating Spy objects in JUnit tests.
What is a risk when using Spy objects in tests?
✗ Incorrect
Since Spies call real methods, those methods might cause side effects or errors if not handled carefully.
How do you verify a method call on a Spy object in Mockito?
✗ Incorrect
Mockito uses the verify() method to check if a method was called on a Spy or Mock.
Which statement is true about Spy objects?
✗ Incorrect
Spies wrap real objects and allow you to mock some methods while keeping others real.
Explain what a Spy object is and when you would use it in testing.
Think about how a Spy lets you check real method calls.
You got /3 concepts.
Describe how to create and verify a Spy object in a JUnit test using Mockito.
Remember the example with a List and verify method call.
You got /3 concepts.