Lesson 50: Testing a Real-Time Order System (Capstone)

20 Aug 2026 1 min Swarnil Singhai

This capstone ties together OOP, collections, streams, exceptions, JDBC, and testing into one small but realistic order-processing flow.

Real-time example

Simulate an order pipeline: validate stock, charge payment, persist the order, and notify the customer — with a test proving it end-to-end.

class OrderPipeline {
    boolean process(Order order, Inventory inv, PaymentGateway pay) {
        if (!inv.hasStock(order.sku(), order.qty()))
            throw new IllegalStateException("Out of stock");
        if (!pay.charge(order.amount()))
            return false;
        inv.reduce(order.sku(), order.qty());
        return true;
    }
}

@Test
void processesValidOrder() {
    var pipeline = new OrderPipeline();
    var inv = new Inventory(Map.of("SKU-1", 10));
    var pay = mock(PaymentGateway.class);
    when(pay.charge(anyDouble())).thenReturn(true);

    assertTrue(pipeline.process(new Order("SKU-1", 2, 99.0), inv, pay));
}

What's happening

Every earlier lesson shows up here: exceptions for bad state, an interface (PaymentGateway) mocked in a real test, and a clean domain method.

This is the shape of real production code — small, focused classes, each testable in isolation, composed into a pipeline.

Advertisement