Sub topic 1
Why Write Unit Tests?
Every program has bugs at some point. The hard part is finding them early, while they are still cheap to fix. Unit tests help you do that.
What Is a Unit Test?
A unit is a small piece of code that does one job. In Java, a unit is usually one method or one class.
A unit test is a short piece of code that runs a unit and checks its result. If the result is what you expected, the test passes. If not, the test fails and tells you what went wrong.
Say you have a method that adds two numbers. A unit test for it might call the method with 2 and 3 and check that the answer is 5.
Checking Code by Hand
Without tests, most people check their code by hand. They run the program, type in some values, and look at the output. That works once. It gets tiring when you have to do it again after every change.
You can write a check in plain Java. This program runs on its own:
public class ManualCheck {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(2, 3);
if (result != 5) {
throw new AssertionError("expected 5 but got " + result);
}
System.out.println("add works");
}
}This is already a simple test, because it checks a result automatically. But it has problems. You need a separate main method for each check. One failure stops everything. Nothing collects a report of what passed and what failed.
What JUnit Adds
JUnit is a free library for writing and running unit tests in Java. This tutorial uses JUnit 5. JUnit finds your tests, runs them all, and gives you a clear report. Your IDE and build tools such as Maven and Gradle know how to run JUnit tests.
| Checking by hand | Automated tests with JUnit |
|---|---|
| Slow, repeated each time | Run hundreds of checks in seconds |
| Easy to forget a case | Every case runs every time |
| Results live in your head | Clear pass or fail report |
| Hard to share | Saved with the code for the whole team |
Why It Matters
Tests give you three main benefits.
- They catch bugs early. A failing test points to the problem a few minutes after you cause it, not weeks later.
- They stop regressions. A regression is a bug in code that used to work. A test you write today keeps checking that feature after every future change.
- They let you change code without fear. When all the tests still pass after you clean up some code, you know you didn't break the behavior they check.
What Comes Next
Next you will add JUnit 5 to a project. Then you will write your first real test.
Exercise
You fix a bug in a method that formats dates. A month later, someone changes the same method and the old bug comes back. What is this kind of bug called, and how would a unit test have helped?
Show answer
It is called a regression, meaning a bug in code that used to work. If you had written a unit test for the fixed case, the test would run after the later change. It would fail right away and show that the old bug was back, before the code reached users.
Exercise
Name two problems with checking code using a separate main method for each check, as in the ManualCheck example.
Show answer
Any two of these: you need a new main method for every check, one failure stops the program so later checks never run, and nothing collects a report of which checks passed and which failed.