Challenge - 5 Problems
Spy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of partial mocking with @Spy
Consider the following JUnit test using Mockito's @Spy annotation. What will be the output of the test method?
JUnit
public class Calculator { public int add(int a, int b) { return a + b; } public int multiply(int a, int b) { return a * b; } } @ExtendWith(MockitoExtension.class) public class CalculatorTest { @Spy Calculator calculator; @Test public void testAddAndMultiply() { when(calculator.multiply(2, 3)).thenReturn(10); int sum = calculator.add(2, 3); int product = calculator.multiply(2, 3); System.out.println("Sum: " + sum); System.out.println("Product: " + product); } }
Attempts:
2 left
💡 Hint
Remember that @Spy calls real methods unless stubbed.
✗ Incorrect
The @Spy annotation creates a partial mock. The add method is not stubbed, so it calls the real method returning 5. The multiply method is stubbed to return 10, so it returns 10 instead of 6.
❓ assertion
intermediate1:30remaining
Correct assertion for verifying @Spy behavior
Given a @Spy object of class Service with a method fetchData(), which assertion correctly verifies that fetchData() was called exactly once?
JUnit
@Spy
Service service;
@Test
public void testFetchData() {
service.fetchData();
// Which assertion is correct here?
}Attempts:
2 left
💡 Hint
Use Mockito's verify method with times() for call count.
✗ Incorrect
The correct way to verify method calls with Mockito is using verify(object, times(n)).method(). Option A uses this correctly. Option A is invalid syntax. Option A is an assertion on return value, not call count. Option A uses a non-existent 'once()' method.
🔧 Debug
advanced2:30remaining
Debugging unexpected behavior with @Spy
A test uses @Spy on a List object. The test stubs the size() method to return 5, but the real size() method is called (returning 0) during stubbing. What is the most likely cause?
JUnit
@Spy
List<String> spyList = new ArrayList<>();
@Test
public void testSize() {
when(spyList.size()).thenReturn(5);
int size = spyList.size();
System.out.println(size);
}Attempts:
2 left
💡 Hint
When stubbing spy methods, some methods require doReturn() style stubbing.
✗ Incorrect
When stubbing methods on spies that are final or have side effects, using when().thenReturn() can call the real method and cause unexpected behavior. Using doReturn().when() avoids calling the real method during stubbing.
🧠 Conceptual
advanced1:30remaining
Understanding @Spy vs @Mock behavior
Which statement best describes the difference between @Spy and @Mock in Mockito?
Attempts:
2 left
💡 Hint
Think about which mock type calls real methods by default.
✗ Incorrect
@Spy creates a partial mock that calls real methods unless you stub them. @Mock creates a full mock where all methods return default values unless stubbed.
❓ framework
expert3:00remaining
Best practice for using @Spy with constructor arguments
You want to create a @Spy instance of a class Service that requires constructor arguments. Which approach correctly initializes the spy in a JUnit 5 test with Mockito?
Attempts:
2 left
💡 Hint
Use field initializer to construct the real instance with args for @Spy.
✗ Incorrect
Option A correctly uses the field initializer to create the Service instance with required constructor arguments, and Mockito spies that pre-constructed instance. Options B and D fail because @Spy on an uninitialized field requires a no-arg constructor for automatic instantiation, which Service lacks. Option A creates a mock, not a spy.