JAVA-220 · Lesson 1 of 12 · 21 min

Why testing matters

▶ Video coming soon A video version of this lesson is on the way.

Hamza's second year began on Thursday, September 9, 2027, with a new course and a new routine. Ingrid now meets him on Thursday afternoons, 1:00 to 3:00, in Second Gear's back office, while the shop is quiet before the evening repair night. She opened her laptop to a list she had kept since the spring. In April the Money class had printed a refund of 50 cents as 0.50 instead of -0.50, and nobody noticed until Leif read the code for the capstone build. In November 2026 PartsDesk crashed when Kofi typed find with no word after it, which Hamza's acceptance tests caught only a day before the demo. "Every one of these was found by a person reading or clicking," she said. "This term you will make the computer find them, every time you build, forever."

This course is about writing tests: small programs that run your code and check that it does what it should. It also covers the habits that keep code easy to test and safe to change: test-driven development, coverage, logging, refactoring and code review. By December, Hamza will put tests around an old, messy part of PartsDesk and then improve it without breaking it.

What a test is

A test is code that calls the code you want to check, the code under test, with chosen inputs and compares what comes back with what you expected. If the two match, the test passes; if not, it fails and says what went wrong. The key property is that a test is self-checking: it decides pass or fail by itself, so nobody has to read output and judge it. A program that prints a total and relies on someone to notice a wrong number is not a test; a program that compares the total with the right number and reports a failure is.

Java Example · Main.java
public class Main {
    static long totalCents(long priceCents, int quantity) {
        return priceCents * quantity;
    }

    public static void main(String[] args) {
        // Printing: a person has to read this and decide whether it is right.
        System.out.println("4 tubes at 899 cents: " + totalCents(899, 4));

        // Checking: the program decides for itself.
        long expected = 3596;
        long actual = totalCents(899, 4);
        if (actual == expected) {
            System.out.println("PASS total for 4 tubes");
        } else {
            System.out.println("FAIL total for 4 tubes: expected " + expected + " but was " + actual);
        }
    }
}
Output
4 tubes at 899 cents: 3596
PASS total for 4 tubes

What this code does

The first line only prints a number, so a person has to know the right answer to spot a mistake. The second part states the expected value and compares it with the actual one, printing PASS or a FAIL message that names both values. That comparison is the heart of every automated test, however large the testing tool around it.

Hamza had written checks like this in his first course, a method called check that printed PASS or FAIL. This term he will replace them with JUnit, the standard testing library for Java, but the idea is the same: arrange some inputs, act by calling the code, and assert that the result is right.

Why automated tests pay for themselves

Testing by hand works once. The trouble is that software keeps changing, and every change can break something that used to work. A bug that comes back after being fixed is called a regression, and the only practical defence against regressions is a set of tests that runs automatically after every change. Writing a test takes a few minutes; running it again costs nothing. A team with good tests can change code with confidence, while a team without them learns to fear touching anything, and its code slowly rots.

  • Tests find bugs early, when they are cheap to fix, instead of at the counter or in a customer's hands.
  • Tests protect against regressions: once a bug has a test, it cannot quietly return.
  • Tests document behaviour: a test named refundPrintsMinusSign says exactly what the code promises.
  • Tests make refactoring safe, because they tell you at once if a cleanup changed behaviour.
  • Tests improve design: code that is hard to test is usually doing too much in one place.
A bar chart of the relative cost of fixing the same bug when it is found at different stages: while writing the code, 1; in a unit test run, 2; in code review, 5; in testing before release, 15; at the counter after release, 50 or more. The bars grow steeply from left to right.
The later a bug is found, the more it costs to fix (illustrative figures)

The figures in the chart are illustrative, not measured, but the shape is real and widely observed. A bug found while you are still writing a method costs seconds. The same bug found by Kofi at the counter costs his time, a confused customer, Hamza's time to reproduce it, a fix, a new release and a check that nothing else broke.

Kinds of tests

Tests are grouped by how much of the system they exercise. A unit test checks one small piece, usually a single class or method, in isolation and in milliseconds. An integration test checks that several pieces work together: a class with a real file, a service with its database, two modules through their real interfaces. An end-to-end test drives the whole program the way a user would, through its console, web page or API. Each kind catches problems the others miss.

Kinds of automated tests
KindWhat it checksSpeedSecond Gear example
Unit testOne class or method in isolationMillisecondsMoney.toString prints -0.50 for -50 cents
Integration testSeveral parts together, often with a real file or databaseTenths of a second to secondsPartsDesk saves the part list to a file and reads it back
End-to-end testThe whole program as a user sees itSeconds to minutesTyping low at the PartsDesk prompt lists the right parts
Acceptance testThat the program meets a stated requirementVariesRequirement R5, the low-stock report, checked with Kofi
A test pyramid. The wide base is labelled unit tests: many, fast, cheap. The middle layer is labelled integration tests: fewer, slower. The narrow top is labelled end-to-end tests: few, slowest, most fragile. An arrow on the right points up and is labelled slower and more expensive to run and maintain.
The test pyramid: many fast unit tests, fewer integration tests and a few end-to-end tests

The test pyramid is a rule of thumb for the mix: many unit tests at the base, fewer integration tests in the middle and a handful of end-to-end tests at the top. Unit tests are fast and pinpoint the broken line; end-to-end tests are slow and break for many reasons, so a project that relies mostly on them gets slow, flaky feedback. The pyramid is a guide, not a law, but it explains why most of this course is about unit tests.

What makes a good test

A useful memory aid is FIRST: good unit tests are Fast, Independent, Repeatable, Self-checking and Timely. Fast means a whole suite runs in seconds, so people run it constantly. Independent means each test sets up what it needs and does not rely on another test having run first. Repeatable means the same result every time, on every machine, with no dependence on the clock, the network or random numbers. Self-checking means pass or fail without a human reading output. Timely means written close to the code, ideally before or with it, not months later.

Java Example · Main.java
import java.util.*;

public class Main {
    static int passed = 0, failed = 0;

    static void check(String name, Object expected, Object actual) {
        if (Objects.equals(expected, actual)) {
            passed++;
        } else {
            failed++;
            System.out.println("FAIL " + name + ": expected <" + expected + "> but was <" + actual + ">");
        }
    }

    // The April 2027 bug: the sign was lost for amounts under one dollar.
    static String oldFormat(long cents) {
        return String.format("%d.%02d", cents / 100, Math.abs(cents % 100));
    }

    static String newFormat(long cents) {
        String sign = cents < 0 ? "-" : "";
        long a = Math.abs(cents);
        return String.format("%s%d.%02d", sign, a / 100, a % 100);
    }

    public static void main(String[] args) {
        long[] inputs = {899, 0, -1250, -50};
        String[] expected = {"8.99", "0.00", "-12.50", "-0.50"};
        for (int i = 0; i < inputs.length; i++) {
            check("old " + inputs[i], expected[i], oldFormat(inputs[i]));
            check("new " + inputs[i], expected[i], newFormat(inputs[i]));
        }
        System.out.println(passed + " passed, " + failed + " failed");
    }
}
Output
FAIL old -50: expected <-0.50> but was <0.50>
7 passed, 1 failed

What this code does

The check method compares an expected and an actual value and counts the result, printing details only when something fails. Running the same four cases against the old and new formatting code shows that only the old code fails, and only for -50 cents, the case nobody had tried. Integer division of -50 by 100 gives 0, which has no sign to print, so a test with a small negative amount was the only way to catch it.

The failing case is the interesting one. The old code was right for 899 cents, for zero and even for -12.50, so a few quick tries by hand would have passed it. The bug lived in one narrow case, a negative amount under one dollar. Choosing test cases is a skill: the edges, such as zero, negative numbers, empty lists, the largest value and the value exactly on a limit, are where bugs collect.

Choosing test cases

For a method like suggestedOrder from PartsDesk, which tops stock up to twice the reorder level, Ingrid asked Hamza to list cases before writing any code. A typical case, a part well below its level. A part exactly on its level, because isLow uses at or below. A part just above the level, which should not be ordered. A count of zero. A level of zero, which Kofi uses for parts he no longer stocks. Each case is one line in a table, and the table is the test.

Java Example · Main.java
public class Main {
    static boolean isLow(int count, int level) { return count <= level; }
    static int suggestedOrder(int count, int level) { return level * 2 - count; }

    public static void main(String[] args) {
        int[][] cases = {
            // count, level, expected isLow (1 or 0), expected order
            {15, 40, 1, 65},
            {40, 40, 1, 40},
            {41, 40, 0, 39},
            {0, 25, 1, 50},
            {0, 0, 1, 0},
        };
        int failures = 0;
        for (int[] c : cases) {
            boolean low = isLow(c[0], c[1]);
            int order = suggestedOrder(c[0], c[1]);
            boolean ok = low == (c[2] == 1) && order == c[3];
            System.out.printf("count %2d level %2d -> low %-5s order %2d  %s%n", c[0], c[1], low, order, ok ? "ok" : "FAIL");
            if (!ok) failures++;
        }
        System.out.println(failures == 0 ? "All cases pass" : failures + " case(s) failed");
    }
}
Output
count 15 level 40 -> low true  order 65  ok
count 40 level 40 -> low true  order 40  ok
count 41 level 40 -> low false order 39  ok
count  0 level 25 -> low true  order 50  ok
count  0 level  0 -> low true  order  0  ok
All cases pass

What this code does

Each row of the table holds the inputs and the expected answers, and the loop runs every row through the two methods. The row with count 41 shows that suggestedOrder still returns 39 even though the part is not low, which is fine because the report only asks for an order when isLow is true. Writing that expectation down makes the assumption visible, so a later change to the report cannot silently break it.

A table of cases like this is called a data-driven or parameterized test, and JUnit has direct support for it, which lesson 3 covers. The table also records a fact about the design: suggestedOrder is only meaningful for low parts. If someone later uses it elsewhere, the test row with count 41 is the place where that decision is written down.

Tests in a build

In a Gradle or Maven project, tests live in src/test/java, beside the code they check in src/main/java, in the same packages, so they can call package-private methods. The build compiles and runs them in the test task or phase, and a failing test fails the build. That rule is the point: a change that breaks a test cannot be packaged or released by accident. Lesson 2 sets this up for the secondgear build.

Where tests live in the secondgear build
secondgear/
  core/
    src/main/java/org/secondgear/core/Money.java
    src/test/java/org/secondgear/core/MoneyTest.java
  partsdesk/
    src/main/java/org/secondgear/parts/PartsDesk.java
    src/test/java/org/secondgear/parts/PartTest.java

What this code does

Each module keeps its tests in src/test/java, in the same package as the class they test. The build compiles the test folder separately, so test code never ends up in the shipped JAR. A test class is usually named after the class it checks with Test added, such as MoneyTest.

Try it yourself

A self-checking test

The method withTax adds tax to a price in cents, rounding to the nearest cent. Complete check so it prints PASS and the name when the values are equal, and FAIL, the name, the expected value and the actual value otherwise.

The program should print one PASS line and one FAIL line, because the last expected value is deliberately wrong.

public class Main {
    static long withTax(long cents, int ratePer100) {
        return Math.round(cents * (100 + ratePer100) / 100.0);
    }

    static void check(String name, long expected, long actual) {
        System.out.println(name);
    }

    public static void main(String[] args) {
        check("1000 cents plus 13 in 100", 1130, withTax(1000, 13));
        check("899 cents plus 8 in 100", 900, withTax(899, 8));
    }
}

Try it yourself

Find the edge case

The method daysLate should return how many days a rental bike was returned late, or 0 if it came back on time or early. It has a bug.

Add test cases to the arrays so that the program finds the bug. It should end by printing 2 passed, 1 failed, with your failing case being a bike returned early (for example returned on day 3 when due on day 5, where the right answer is 0).

public class Main {
    static int daysLate(int dueDay, int returnedDay) {
        return returnedDay - dueDay;
    }

    public static void main(String[] args) {
        int[] due = {5, 5};
        int[] returned = {7, 5};
        int[] expected = {2, 0};
        int passed = 0, failed = 0;
        for (int i = 0; i < due.length; i++) {
            int actual = daysLate(due[i], returned[i]);
            if (actual == expected[i]) passed++;
            else {
                failed++;
                System.out.println("FAIL due " + due[i] + " returned " + returned[i] + ": expected " + expected[i] + " but was " + actual);
            }
        }
        System.out.println(passed + " passed, " + failed + " failed");
    }
}

Common mistakes

  • MistakeCalling a program tested because it ran and printed something.

    InsteadA test must check the result against an expected value and report pass or fail by itself.

  • MistakeTesting only the typical case.

    InsteadAdd edge cases: zero, negative values, empty input, values exactly on a limit and the largest values.

  • MistakeWriting tests months after the code, if at all.

    InsteadWrite tests with the code, while you still remember what it should do.

  • MistakeRelying mainly on slow end-to-end tests.

    InsteadFollow the test pyramid: many fast unit tests, fewer integration tests and a few end-to-end tests.

  • MistakeTests that depend on each other or on the clock.

    InsteadMake each test independent and repeatable, so it gives the same result every time.

Summary and key terms

  • A test runs the code under test with chosen inputs and compares the result with an expected value, deciding pass or fail by itself.
  • Automated tests find bugs early, guard against regressions, document behaviour and make change safe.
  • Unit tests check one class or method in milliseconds, integration tests check parts working together, and end-to-end tests drive the whole program.
  • The test pyramid suggests many unit tests, fewer integration tests and few end-to-end tests.
  • Good tests are fast, independent, repeatable, self-checking and timely.
  • Bugs cluster at the edges, so test cases should include zero, negatives, limits and empty input.
  • In a build, tests live in src/test/java and a failing test fails the build.
Test
Code that runs other code with chosen inputs and checks the result against an expected value.
Code under test
The class or method a test is checking.
Self-checking
Deciding pass or fail automatically, without a person reading the output.
Regression
A bug that returns, or a feature that breaks, after a change.
Unit test
A fast test of one small piece of code in isolation.
Integration test
A test of several parts working together, often with real files or databases.
End-to-end test
A test that drives the whole program as a user would.
Test pyramid
The guideline of many unit tests, fewer integration tests and few end-to-end tests.
FIRST
Fast, Independent, Repeatable, Self-checking and Timely: the qualities of good unit tests.
Edge case
An input at the boundary of what the code handles, such as zero or a limit.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. What makes a test self-checking?

    Show answer

    It compares the actual result with an expected value and reports pass or fail itself.

  2. What is a regression?

    Show answer

    A bug that comes back, or something that breaks, after a change.

  3. Why did quick checks by hand miss the Money sign bug?

    Show answer

    It only showed for negative amounts under one dollar, an edge case nobody tried.

  4. Where do tests live in a Gradle project?

    Show answer

    In src/test/java, in the same package as the code they test.

  5. What does the R in FIRST stand for?

    Show answer

    Repeatable: the same result every time, on every machine.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz