0
0
JUnittesting~20 mins

Spy objects 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!
🧠 Conceptual
intermediate
1:30remaining
Purpose of Spy Objects in Unit Testing
What is the primary purpose of using a spy object in unit testing with JUnit?
ATo verify interactions with real objects while allowing actual method calls
BTo simulate exceptions without calling any real methods
CTo replace the entire implementation of a class with dummy methods
DTo generate random test data automatically during tests
Attempts:
2 left
💡 Hint
Think about how spies differ from mocks in terms of method calls.
Predict Output
intermediate
2: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);
    }
}
ATest throws NullPointerException during spy creation
BTest passes successfully with both verifications and assertion true
CTest fails due to assertion error on spyList size
DTest fails because verify does not work on spy objects
Attempts:
2 left
💡 Hint
Remember that spy calls real methods unless stubbed.
assertion
advanced
1: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?
    }
}
Averify(calc, atLeast(4)).add(anyInt(), anyInt());
Bverify(calc, times(1)).add(anyInt(), anyInt());
Cverify(calc, never()).add(anyInt(), anyInt());
Dverify(calc, times(3)).add(anyInt(), anyInt());
Attempts:
2 left
💡 Hint
Count how many times add() is called in the test.
🔧 Debug
advanced
2: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);
    }
}
ANullPointerException occurs because getData() returns null by default
BSpy object cannot stub any methods, only mocks can
CUsing when() on spy calls real method causing NullPointerException; use doReturn() instead
DTest fails because spy was not initialized with MockitoAnnotations
Attempts:
2 left
💡 Hint
Consider how Mockito handles stubbing on spies differently than mocks.
framework
expert
2:30remaining
Spy Object Behavior in Integration Testing
In a JUnit integration test using Mockito spies, which statement about spy objects is TRUE?
ASpies allow partial mocking, so real methods run unless stubbed, useful for verifying side effects in integration tests
BSpies completely replace the real object, so no real methods are executed during integration tests
CSpies automatically reset their state after each test without additional configuration
DSpies cannot be used in integration tests because they only work with unit tests
Attempts:
2 left
💡 Hint
Think about how spies differ from mocks and their use cases beyond unit tests.