Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 10 / Topic 9 of 12

Test doubles by hand

BankAccount.withdraw sends a low-balance alert through an AlertService. A real AlertService might send an SMS or an email — something a test must never actually do (slow, costs money, needs a working network, and tells you nothing about whether BankAccount's logic is correct). This course has not introduced Mockito or any mocking library, and does not need one yet: a hand-written test double is just a small class that implements the same interface and does something safe and observable instead.

public interface AlertService {
    void sendLowBalanceAlert(String owner, double balance);
}
public class ConsoleAlertService implements AlertService {
    @Override
    public void sendLowBalanceAlert(String owner, double balance) {
        System.out.printf("ALERT: %s's balance is low: Rs %.2f%n", owner, balance);
    }
}
import java.util.ArrayList;
import java.util.List;

public class FakeAlertService implements AlertService {

    private final List<String> alertsSent = new ArrayList<>();

    @Override
    public void sendLowBalanceAlert(String owner, double balance) {
        alertsSent.add(owner + ":" + balance);
    }

    public List<String> alertsSent() {
        return alertsSent;
    }

    public boolean wasAlertSentFor(String owner) {
        return alertsSent.stream().anyMatch(entry -> entry.startsWith(owner + ":"));
    }
}

ConsoleAlertService is the real implementation — used by LoggingDemo in Section 11, never by a test. FakeAlertService sends no real alert at all; it just remembers what it was asked to send, in a plain List, so a test can later ask "was the right alert sent?":

@Test
void withdraw_takesBalanceBelowThreshold_sendsOneLowBalanceAlert() {
    account.withdraw(600.0); // 1000 - 600 = 400, below the 500 threshold

    assertEquals(1, alertService.alertsSent().size());
    assertTrue(alertService.wasAlertSentFor("Asha"));
}

@Test
void withdraw_staysAboveThreshold_sendsNoAlert() {
    account.withdraw(200.0); // 1000 - 200 = 800, still above the threshold

    assertFalse(alertService.wasAlertSentFor("Asha"));
    assertEquals(0, alertService.alertsSent().size());
}

The one design choice that makes any of this possible is in BankAccount's constructor: AlertService is passed in, not created with new ConsoleAlertService() inside the class. That seam — a dependency arriving from outside instead of being hard-wired — is the entire trick behind every test double, hand-written or library-generated. A stub returns canned answers and nothing more; the fake here goes one step further and records what happened, which is sometimes also called a spy — the vocabulary varies by book, the technique (write a small class implementing the same interface) is identical either way. A real mocking library (Mockito, in a later Stage 3/5 context) builds exactly this kind of class for you, faster, once a project has dozens of interfaces to fake — worth knowing it exists, not worth reaching for yet while one hand-written class still reads this clearly.

Full programs: examples/bank-and-discount/AlertService.java, examples/bank-and-discount/ConsoleAlertService.java, examples/bank-and-discount/FakeAlertService.java, examples/bank-and-discount/BankAccount.java.

Done here? The practice set for Part 10 — including the solutions — is on the part page.

Search the guide