Sub topic 16
Writing Tests That Stay Useful
A test is useful when it fails only for a real reason and tells you clearly what broke. A badly written test does the opposite. It fails at random, runs slowly, or breaks every time you tidy up your code. This chapter covers a few habits that keep your tests worth having.
The FIRST Rules
Many developers remember good test habits with the word FIRST.
| Letter | Rule | What it means |
|---|---|---|
| F | Fast | Each test runs in milliseconds, so you can run them all often. |
| I | Independent | No test depends on another test running first. |
| R | Repeatable | A test gives the same result every time, on any machine. |
| S | Self-checking | The test passes or fails on its own. Nobody reads printed output. |
| T | Timely | You write the test close to the time you write the code. |
A flaky test is a test that sometimes passes and sometimes fails when nothing in the code has changed. Flaky tests break the "Repeatable" rule. People soon stop trusting them, and then the tests stop protecting anything.
Worked Example: Removing a Hidden Input
Here is a class that is hard to test well:
import java.time.LocalTime;
public class Greeter {
public String greet() {
int hour = LocalTime.now().getHour();
if (hour < 12) {
return "Good morning";
}
return "Good afternoon";
}
}A test of greet() would pass in the morning and fail after lunch. The current time is a hidden input, a value the code uses that the test cannot control.
The fix is to pass the hour in as a parameter. Then the test controls every input:
public class Greeter {
public String greet(int hour) {
if (hour < 12) {
return "Good morning";
}
return "Good afternoon";
}
}import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class GreeterTest {
private final Greeter greeter = new Greeter();
@Test
void greetsMorningBeforeNoon() {
assertEquals("Good morning", greeter.greet(9));
}
@Test
void greetsAfternoonFromNoon() {
assertEquals("Good afternoon", greeter.greet(12));
}
}These tests are fast and repeatable. They also check the edge case at hour 12. An edge case is a value right at the boundary where the behavior changes.
More Habits That Help
- Test one behavior per test. If a test named
greetsMorningBeforeNoonfails, you know straight away what broke. - Test what the code does, not how it does it. Check the result, not private details. Then you can rewrite the inside of a method without breaking your tests.
- Avoid shared changing state. A
staticfield that one test changes can make another test fail. Give each test its own fresh objects. - Keep slow things out. Real databases, networks, and clocks make tests slow and unpredictable. Pass values in, or use a mock.
- Watch a new test fail once. If it never fails, it might not be checking anything.
Exercise
A test creates a file named "report.txt" in the project folder and never deletes it. A second test fails if that file already exists. Which FIRST rule is broken, and how could you fix it?
Show answer
The "Independent" rule is broken, because the second test's result depends on whether the first test ran. Fix it by writing the file into a @TempDir directory, which gives each test its own clean folder and removes it afterward.
Exercise
Why is it better to check the return value of a method than to check the values of its private fields?
Show answer
The return value is the behavior other code depends on. Private fields are implementation details. If the test checks them, it breaks whenever you restructure the class, even when the behavior is still correct.