Stop 11 · Git, Build Tools & TestingTopics · 8/10

Test-Driven Development in Java

Part of the Java Roadmap

In short

Writing tests after the code often feels like a chore. TDD flips it: the test comes first and tells you exactly what to build, so you write less code and every line is already tested.

TDD means writing a failing JUnit test first, writing just enough code to pass it, then refactoring while the tests stay green. It leads to small, well-tested units and clearer designs.

The jaadu as a tailor wraps a measuring tape around a customer's shoulders before touching the uncut cloth on the table.

Think of it like this

The tailor's measurements

A good darzi takes your measurements before cutting any cloth. Then he cuts, tries the kurta on you, and adjusts until it fits. He never stitches first and hopes. In TDD the failing test is the measurement, the code is the cut, and refactoring is the final tidy stitching.

Watch it happen

One TDD cycle

Red
Green
Refactor

1/4Write one small test for the next behaviour: orders below ₹499 pay ₹40 delivery. Run it. It fails, because DeliveryCharge doesn't exist yet.

The big ideas

What you need to know

Tiny steps

Each cycle adds one behaviour and takes a few minutes. Small steps mean you always know what broke.

See it fail first

A test that never failed might not test anything. Watching it go red proves it checks the right thing.

Refactor on green only

Change structure only when all tests pass, so the tests catch any mistake you make while tidying.

Design feedback

If a class is hard to test, it is usually doing too much or creating its own dependencies. TDD exposes that early.

See it in code

Walk through the code

java
1// Step 1 (RED): the test is written before DeliveryCharge exists2class DeliveryChargeTest {3    private final DeliveryCharge charge = new DeliveryCharge();4 5    @Test6    void ordersBelow499PayForty() {7        assertEquals(40, charge.forOrder(300));8    }9 10    @Test11    void ordersOf499OrMoreAreFree() {12        assertEquals(0, charge.forOrder(499));13        assertEquals(0, charge.forOrder(1200));14    }15}16 17// Steps 2 and 3 (GREEN, then REFACTOR): simplest code, then named constants18class DeliveryCharge {19    private static final int FREE_FROM = 499;20    private static final int FLAT_FEE = 40;21 22    int forOrder(int orderTotal) {23        return orderTotal >= FREE_FROM ? 0 : FLAT_FEE;24    }25}

1/4Cycle one: written first, it fails to compile, which counts as red. Then the first version was simply return 40;

Two TDD cycles for a delivery charge rule. Imports for @Test and assertEquals are left out to keep it short.

Save yourself the pain

Common mistakes

Oops

Writing ten tests up front, then all the code.

Do this instead

One failing test at a time. Make it pass, tidy up, then write the next.

Oops

Skipping the refactor step because the tests are green.

Do this instead

Refactoring is where the design improves. Green is the safe moment to do it.

Oops

Testing private methods directly.

Do this instead

Test through the public behaviour. Private methods are details the refactor step should be free to change.

Your mission

TDD kata: UPI amount validator

~45 min

Quick check

In TDD, what should you do right after writing a new test?

Go deeper

  1. 1Build a small PasswordValidator class entirely with red, green, refactor cycles.
  2. 2Keep each cycle to a few minutes and commit after each green step.
  3. 3Notice how writing tests first changes the shape of your methods.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge