Challenge - 5 Problems
Spy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Purpose of Spy Objects in Unit Testing
What is the primary purpose of using a spy object in unit testing with JUnit?
Attempts:
2 left
💡 Hint
Think about how spies differ from mocks in terms of method calls.
✗ Incorrect
A spy object wraps a real object and allows you to verify method calls while still executing the actual methods. This helps in testing side effects and interactions.
❓ Predict Output
intermediate2:00remaining
Output of Spy Verification in JUnit
What will be the result of running this JUnit test using Mockito spy?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; public class SpyTest { @Test public void testSpyList() { List<String> list = new ArrayList<>(); List<String> spyList = spy(list); spyList.add("one"); spyList.add("two"); verify(spyList).add("one"); verify(spyList).add("two"); assert(spyList.size() == 2); } }
Attempts:
2 left
💡 Hint
Remember that spy calls real methods unless stubbed.
✗ Incorrect
The spy wraps the real list, so add() calls actually add elements. Verifications confirm the calls happened. The size is 2, so assertion passes.
❓ assertion
advanced1:30remaining
Correct Assertion for Spy Method Call Count
Which assertion correctly verifies that a spy object's method was called exactly three times?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; public class SpyCallCountTest { public static class Calculator { public int add(int a, int b) { return a + b; } } @Test public void testAddCallCount() { Calculator calc = spy(new Calculator()); calc.add(1, 2); calc.add(3, 4); calc.add(5, 6); // Which verify statement is correct? } }
Attempts:
2 left
💡 Hint
Count how many times add() is called in the test.
✗ Incorrect
The add method is called three times, so verify with times(3) is correct.
🔧 Debug
advanced2:00remaining
Debugging Spy Stubbing Issue
Why does the following test fail with a NullPointerException when stubbing a spy method?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; public class SpyStubTest { public static class Service { public String getData() { return "real data"; } } @Test public void testStubSpy() { Service service = spy(new Service()); when(service.getData()).thenReturn(null); String result = service.getData(); assert(result == null); } }
Attempts:
2 left
💡 Hint
Consider how Mockito handles stubbing on spies differently than mocks.
✗ Incorrect
When stubbing spy methods, using when() calls the real method immediately, which can cause errors. Using doReturn() avoids calling the real method.
❓ framework
expert2:30remaining
Spy Object Behavior in Integration Testing
In a JUnit integration test using Mockito spies, which statement about spy objects is TRUE?
Attempts:
2 left
💡 Hint
Think about how spies differ from mocks and their use cases beyond unit tests.
✗ Incorrect
Spies wrap real objects and allow real methods to run unless stubbed, making them useful to verify behavior and side effects in integration tests.