Why advanced mocking handles complex dependencies in JUnit - Automation Benefits in Action
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class ComplexServiceTest { @Mock DependencyServiceA serviceA; @Mock DependencyServiceB serviceB; @InjectMocks ComplexService complexService; @Test void testComplexServiceMethod() { // Arrange: stub dependent services when(serviceA.getData()).thenReturn("dataA"); when(serviceB.process("dataA")).thenReturn("processedDataB"); // Act: call method under test String result = complexService.combineData(); // Assert: verify result and interactions assertEquals("Result: processedDataB", result); verify(serviceA).getData(); verify(serviceB).process("dataA"); } } // Supporting classes for context class ComplexService { private final DependencyServiceA serviceA; private final DependencyServiceB serviceB; public ComplexService(DependencyServiceA serviceA, DependencyServiceB serviceB) { this.serviceA = serviceA; this.serviceB = serviceB; } public String combineData() { String data = serviceA.getData(); String processed = serviceB.process(data); return "Result: " + processed; } } interface DependencyServiceA { String getData(); } interface DependencyServiceB { String process(String input); }
This test uses Mockito with JUnit 5 to handle complex dependencies.
We use @Mock to create mocks for the two dependent services. @InjectMocks injects these mocks into the service under test.
We stub the dependent services' methods to return controlled values using when().thenReturn(). This simulates complex interactions without needing real implementations.
The test calls the service method and asserts the returned value matches the expected result based on the mocks.
Finally, verify() checks that the dependent services were called correctly, ensuring the service interacts properly with its dependencies.
This approach isolates the service logic and handles complex dependencies cleanly and reliably.
Now add data-driven testing with 3 different input scenarios for the dependent services