0
0
JUnittesting~10 mins

Why advanced mocking handles complex dependencies in JUnit - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test verifies that advanced mocking can handle complex dependencies by simulating interactions between multiple dependent objects in a service method.

Test Code - JUnit 5 with Mockito
JUnit
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

class OrderServiceTest {

    @Mock
    InventoryService inventoryService;

    @Mock
    PaymentService paymentService;

    @InjectMocks
    OrderService orderService;

    @BeforeEach
    void setup() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    void testPlaceOrder_Success() {
        // Arrange
        String productId = "prod123";
        int quantity = 2;
        when(inventoryService.isAvailable(productId, quantity)).thenReturn(true);
        when(paymentService.charge(anyDouble())).thenReturn(true);

        // Act
        boolean result = orderService.placeOrder(productId, quantity, 50.0);

        // Assert
        assertTrue(result);
        verify(inventoryService).isAvailable(productId, quantity);
        verify(paymentService).charge(50.0);
    }
}

// Supporting classes
class OrderService {
    private final InventoryService inventoryService;
    private final PaymentService paymentService;

    public OrderService(InventoryService inventoryService, PaymentService paymentService) {
        this.inventoryService = inventoryService;
        this.paymentService = paymentService;
    }

    public boolean placeOrder(String productId, int quantity, double amount) {
        if (!inventoryService.isAvailable(productId, quantity)) {
            return false;
        }
        return paymentService.charge(amount);
    }
}

interface InventoryService {
    boolean isAvailable(String productId, int quantity);
}

interface PaymentService {
    boolean charge(double amount);
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Mockito annotations are initializedMocks for InventoryService and PaymentService are created and injected into OrderService-PASS
2Test calls orderService.placeOrder with productId 'prod123', quantity 2, amount 50.0OrderService calls inventoryService.isAvailable with 'prod123' and 2-PASS
3Mocked inventoryService returns true for availabilityOrderService proceeds to call paymentService.charge with 50.0-PASS
4Mocked paymentService returns true for chargeOrderService returns true indicating order placed successfullyassertTrue(result) verifies order placement successPASS
5Verify inventoryService.isAvailable was called with correct parametersVerification passes confirming interactionverify(inventoryService).isAvailable(productId, quantity)PASS
6Verify paymentService.charge was called with correct amountVerification passes confirming interactionverify(paymentService).charge(50.0)PASS
Failure Scenario
Failing Condition: inventoryService mock returns false indicating product not available
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the orderService.placeOrder method?
AIt verifies that placeOrder throws an exception on payment failure
BIt verifies that placeOrder returns true when inventory is available and payment succeeds
CIt verifies that placeOrder updates the database directly
DIt verifies that placeOrder calls external APIs without mocks
Key Result
Using advanced mocking allows testing service methods with complex dependencies by simulating their behavior, ensuring tests are isolated and reliable.