0
0
JUnittesting~20 mins

@Spy for partial mocking in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ASum: 0\nProduct: 6
BSum: 5\nProduct: 10
CSum: 0\nProduct: 10
DSum: 5\nProduct: 6
Attempts:
2 left
💡 Hint
Remember that @Spy calls real methods unless stubbed.
assertion
intermediate
1: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?
}
Averify(service, times(1)).fetchData();
Bverify(service).fetchData(1);
CassertEquals(1, service.fetchData());
Dverify(service, once()).fetchData();
Attempts:
2 left
💡 Hint
Use Mockito's verify method with times() for call count.
🔧 Debug
advanced
2: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);
}
AThe spyList was not initialized properly before stubbing.
BThe when() method was called before the spy was created.
CThe stubbing should use doReturn(5).when(spyList).size() instead of when().thenReturn().
DThe size() method cannot be stubbed on a spy.
Attempts:
2 left
💡 Hint
When stubbing spy methods, some methods require doReturn() style stubbing.
🧠 Conceptual
advanced
1:30remaining
Understanding @Spy vs @Mock behavior
Which statement best describes the difference between @Spy and @Mock in Mockito?
A@Spy creates a full mock with no real method calls; @Mock creates a partial mock that calls real methods unless stubbed.
B@Spy disables all method calls; @Mock calls real methods by default.
C@Spy and @Mock behave identically; both call real methods unless stubbed.
D@Spy creates a partial mock that calls real methods unless stubbed; @Mock creates a full mock with no real method calls.
Attempts:
2 left
💡 Hint
Think about which mock type calls real methods by default.
framework
expert
3: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?
A
@Spy
Service service = new Service("arg1", 10);
B
@Spy
Service service;

@BeforeEach
public void setup() {
    service = spy(new Service("arg1", 10));
}
C
@Spy
Service service = mock(Service.class);
D
@Spy
Service service;

@BeforeEach
public void setup() {
    service = mock(Service.class, withSettings().useConstructor("arg1", 10).defaultAnswer(CALLS_REAL_METHODS));
}
Attempts:
2 left
💡 Hint
Use field initializer to construct the real instance with args for @Spy.