Discover how to speed up tests without losing real behavior accuracy!
Why @Spy for partial mocking in JUnit? - Purpose & Use Cases
Imagine testing a complex calculator app where you want to check only the addition method but the subtraction method calls a slow external service.
Manually running tests means you either wait for the slow service every time or skip testing parts, risking bugs. It's slow, frustrating, and error-prone.
@Spy lets you keep the real behavior for most methods but replace just the slow ones with fake responses. This way, tests run fast and focus only on what matters.
Calculator calc = new Calculator(); int result = calc.add(2, 3); // calls real add int slow = calc.subtract(5, 2); // calls slow external service
@Spy Calculator calc = new Calculator(); doReturn(3).when(calc).subtract(5, 2); // fake subtract int result = calc.add(2, 3); // real add used
It enables fast, focused tests by mixing real and fake behaviors seamlessly.
Testing a payment system where you want to verify fee calculation but avoid calling the real bank API every time.
@Spy helps test parts of code while faking others.
It saves time by avoiding slow or unreliable calls.
It keeps tests accurate and easier to maintain.
Practice
@Spy annotation do in JUnit testing?Solution
Step 1: Understand @Spy purpose
@Spy creates a real instance of the class but allows selective method overriding.Step 2: Compare with other mocking types
Unlike full mocks, spies call real methods unless stubbed.Final Answer:
Creates a real object but allows overriding some methods for partial mocking. -> Option CQuick Check:
@Spy = Partial real object with override [OK]
- Confusing @Spy with @Mock which creates full fake objects
- Thinking @Spy mocks static methods
- Assuming @Spy runs tests automatically
Solution
Step 1: Recognize proper spy initialization
Using @Spy requires calling MockitoAnnotations.openMocks(this) to initialize the spy fields.Step 2: Check other options
@Spy MyClass mySpy = new MyClass(); misses initialization call; C uses mock instead of spy; D tries spying on null which is invalid.Final Answer:
@Spy MyClass mySpy; MockitoAnnotations.openMocks(this); -> Option AQuick Check:
Initialize spies with openMocks(this) [OK]
- Not calling MockitoAnnotations.openMocks(this)
- Confusing spy creation with mock creation
- Trying to spy on null or uninitialized objects
@Spy
List<String> spyList = new ArrayList<>();
@Test
void testSpyList() {
spyList.add("one");
spyList.add("two");
Mockito.doReturn(100).when(spyList).size();
System.out.println(spyList.size());
}Solution
Step 1: Understand spy behavior with doReturn
The spy calls real methods unless stubbed. Here, size() is stubbed to return 100.Step 2: Analyze output of spyList.size()
Despite adding two elements, size() returns 100 due to stubbing.Final Answer:
100 -> Option BQuick Check:
Stubbed spy method returns stub value [OK]
- Expecting real method size() to return actual list size
- Using when(spyList.size()) instead of doReturn for spies
- Forgetting spy initialization causing NullPointerException
@Spy
MyService service;
@Test
void testService() {
service = new MyService();
Mockito.doReturn("mocked").when(service).getData();
assertEquals("mocked", service.getData());
}Solution
Step 1: Check spy initialization
The @Spy field must be initialized by calling MockitoAnnotations.openMocks(this) before use.Step 2: Analyze assignment and usage
Assigning new instance manually is allowed but without initialization, spy won't work properly.Final Answer:
The spy object is not initialized with MockitoAnnotations.openMocks(this). -> Option AQuick Check:
Always init @Spy with openMocks(this) [OK]
- Forgetting to call MockitoAnnotations.openMocks(this)
- Thinking manual assignment replaces initialization
- Believing doReturn can't be used with spies
PaymentProcessor to test only the processPayment() method while keeping other methods real. Which approach using @Spy is best to achieve this?Solution
Step 1: Understand partial mocking goal
Partial mocking means real methods run except those stubbed.Step 2: Choose spy with selective stubbing
Using @Spy and stubbing only processPayment() lets you test that method while others behave normally.Step 3: Evaluate other options
Mocking entire class loses real behavior; overriding all except one is complex; no spy means no partial mocking.Final Answer:
Create a spy of PaymentProcessor and stub only processPayment() method with Mockito.doReturn(). -> Option DQuick Check:
@Spy + stub target method = partial mocking [OK]
- Mocking entire class losing real method calls
- Overriding too many methods manually
- Not using @Spy for partial mocking
Ready to prove it?
Pass the test and this pattern moves unknown
