A test suite becomes easier to extend when inputs and expected results are visible together. Table-driven tests run the same assertion over several named cases. The table is data, while the loop contains the test procedure. No external test framework is needed to practice this structure.
Understand the contract
Define a record Case with a readable name, an input and an expected answer. Iterate over cases and compare the actual answer with the fixed expectation. If a case fails, its name identifies the contract that broke. Avoid computing expected by calling the implementation again: that would reproduce the defect instead of checking it.
Choose cases from boundaries and distinct decisions. For a range of 1–100 inclusive, 1 and 100 are accepted, 0 and 101 are rejected. More random middle values would add less evidence than those four deliberate cases. A table does not remove the need to think about what could go wrong.
Rejection tests need a different procedure from ordinary return-value tests. Call the operation, fail explicitly if it returns, and catch only the expected exception. The test's own AssertionError must escape. Do not wrap a whole suite in catch(Throwable), which could hide the exact failure you wanted to expose.
Before running, write the output you expect and explain which rule produces each line. Each editor is an independent program.
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Case> cases = List.of(
new Case("minimum", 1, true),
new Case("maximum", 100, true),
new Case("below", 0, false),
new Case("above", 101, false)
);
for (Case test : cases) {
if (allowed(test.value()) != test.expected()) {
throw new AssertionError(test.name());
}
}
System.out.println("4 cases passed");
}
static boolean allowed(int value) {
return value >= 1 && value <= 100;
}
}
record Case(String name, int value, boolean expected) {}Check your prediction
4 cases passedTrace a different case
Testing state after rejection adds information that merely observing an exception cannot provide. Below a snapshot is compared with the original after the attempted invalid update. The method validates before put; moving that guard below put would still throw but would corrupt the map. A useful test should distinguish those implementations.
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> values = new LinkedHashMap<>();
values.put("A", 1);
Map<String, Integer> before = new LinkedHashMap<>(values);
try {
add(values, "A", 99);
throw new AssertionError("duplicate accepted");
} catch (IllegalArgumentException expected) {
if (!values.equals(before)) {
throw new AssertionError("rejection changed state");
}
}
System.out.println("State preserved");
}
static void add(Map<String, Integer> values, String id, int value) {
if (values.containsKey(id)) {
throw new IllegalArgumentException("duplicate");
}
values.put(id, value);
}
}Check your prediction
State preservedChoose cases by decisions rather than quantity
A case table is a compact specification. Each row should identify a distinct boundary or path worth protecting. Four carefully chosen range cases can reveal more than forty arbitrary middle values. Include a readable name so a failure points back to the promise that needs attention.
Keep the test procedure separate from the input/expected data. For state-changing operations, construct a fresh owner per case or explicitly test a sequence of operations. Accidental leftover state from an earlier row can make an otherwise correct implementation appear inconsistent.
Rejection tests need a different expectation from ordinary return values: the call must fail, and state afterward must still meet the contract. A test that catches its own AssertionError can print success without proving either. Inspect the actual failure category during deliberate mutation practice.
Build it yourself
Table-Driven Tests: practice
Create a case table for normalize(String) accepting nonnull nonblank text and returning trimmed text. Include unchanged text, spaces on both sides and a single character. Separately verify null and blanks throw IllegalArgumentException. Print Normalization checks passed. Give each successful case a descriptive name.
public class Main {
public static void main(String[] args) {
// Add your implementation and checks below.
System.out.println("Your workspace");
}
}Hint 1: the idea
The success table can carry expected text, while a separate loop handles rejected inputs.
Hint 2: a first step
Use equals for the actual/expected string comparison and an explicit failure after an unexpectedly successful rejected call.
Show one solution
import java.util.List;
public class Main {
public static void main(String[] args) {
for (Case test : List.of(new Case("plain", "Java", "Java"), new Case("padded", " Java ", "Java"), new Case("single", " A ", "A"))) {
if (!normalize(test.input()).equals(test.expected())) {
throw new AssertionError(test.name());
}
}
for (String invalid : new String[] {null, " "}) {
try {
normalize(invalid);
throw new AssertionError("invalid accepted");
} catch (IllegalArgumentException expected) {
// Expected rejection.
}
}
System.out.println("Normalization checks passed");
}
static String normalize(String input) {
if (input == null || input.trim().isEmpty()) {
throw new IllegalArgumentException("text required");
}
return input.trim();
}
}
record Case(String name, String input, String expected) {}Expected output:
Normalization checks passedEach success case is a record with a name, so a failure reports "padded" rather than a row number. Rejections are kept out of the table because they check a different outcome: an exception, not a return value. The single-character case guards against an implementation that trims by cutting a fixed number of characters.
Change it and test again
Break trimming and confirm the padded case identifies the failure. Then remove blank rejection and ensure the rejection loop fails rather than quietly continuing. Keep test setup independent per case when a future method changes state; a previous successful case should not accidentally prepare the next one.
Explain before moving on
What makes a test expectation independent? Why can two tests with different input strings still exercise the same decision? Organize your suite by behavioral promises so a future reader can tell what is covered and what remains untested.
Completion is saved in this browser only. It does not measure mastery.