java · level 3 · unit 7 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A proves one small piece works.
Professionals do not hope their code works; they prove it. A unit test runs one small piece, a single method, on input whose answer you already know, and compares. Run every test after each change and you find out within seconds when something breaks.
Big frameworks like JUnit exist, but the heart of each is an : this value must equal that one. A tiny check(name, actual, expected) method that prints PASS or FAIL does the same job here.
Test the edge cases hardest: empty text, zero, a negative number, the biggest value allowed. For bad input, test that the method throws, by catching the exception inside the test.
public class Main {
static void check(String name, Object actual, Object expected) {
if (actual.equals(expected)) {
System.out.println("PASS " + name);
} else {
System.out.println("FAIL " + name + ": got " + actual + ", expected " + expected);
}
}
static String initials(String fullName) {
StringBuilder result = new StringBuilder();
for (String part : fullName.trim().split(" ")) {
if (!part.isEmpty()) {
result.append(part.substring(0, 1).toUpperCase());
}
}
return result.toString();
}
public static void main(String[] args) {
check("two names", initials("ada lovelace"), "AL");
check("extra spaces", initials(" grace hopper "), "GH");
check("empty", initials(""), "");
}
} Each test has a name that says what it proves, so a FAIL line tells you where to look.
guess first
Three tests, one bug. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
public class Main {
static void check(String name, Object actual, Object expected) {
if (actual.equals(expected)) {
System.out.println("PASS " + name);
} else {
System.out.println("FAIL " + name + ": got " + actual);
}
}
static int pagesNeeded(int items, int perPage) {
return items / perPage;
}
public static void main(String[] args) {
check("full pages", pagesNeeded(20, 10), 2);
check("part page", pagesNeeded(25, 10), 3);
check("no items", pagesNeeded(0, 10), 0);
}
}
your turn
Your turn. Test the bad input.
On line 19, pick an age, written as text, that should be refused for being too big. NumberFormatException counts as a pass too, because it is a kind of IllegalArgumentException.
output
press run to see what happens
fix it
A test caught a leap year bug. Can you fix the method?
output
press run to see what happens
- The 1900 test fails. Which rule about century years is the method missing?
- Years divisible by 100 are not leap years, unless they are also divisible by 400.
- Change line 10 to
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);, then run.
nailed it
your code now comes with proof. :D
- A unit test checks one small piece against a known answer.
- Edge cases like empty, zero and limits catch the sneakiest bugs.
- Test bad input too: the right exception is a pass.
new words in your collection
- unit test
- assertion
- edge case
This lesson isn’t saved. A free account remembers every lesson you finish.