JUnit 5 from zero: first tests, set-up methods and assertions
Write a first JUnit 5 test in Java, learn the few annotations that matter, use assertions that show clear messages, and check that a call fails in the right way.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: an exam with a marking scheme. Each question has the right answer on the scheme. The marker writes a note when the answer differs. A clean desk is set before each question.
Tom uses JUnit Jupiter to check synthetic Daily List Java behavior. A describes a case, lifecycle setup prepares the intended state, and an assertion compares an observation with a declared .
The marking-scheme picture separates preparing a clean desk from deciding whether an answer meets the rule. A method that only prints a value has not applied that rule.
The picture has a limit. A fresh test object does not reset static fields, files or a database. Test independence requires controlled dependencies and data; a different outcome when run alone is evidence to investigate, not proof of one universal cause.
(SYNTHETIC / TRAINING ONLY: Daily List and every named person are invented.)
PLAIN WORDS · 1 in this section
- junit
- a file format for test results
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Execute independent Jupiter cases with meaningful expected-result assertions and deliberate lifecycle setup.
- Set up
- Check
- Read
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| A question | A test method |
| The correct answer on the scheme | An assertion |
| A fresh desk for each question | Set-up with @BeforeEach |
| A note when wrong | A failure message |
WHERE THIS PICTURE STOPS
- A fresh test object does not reset static, file or external state.
- Different isolated/suite outcomes require investigation; the symptom alone does not prove one cause.
HOOK
Fresh desk, then check.
TELL A FRIEND
An exam: fresh desk, then compare.
OFTEN MIXED UP WITH Your first scripted API check: from a manual request to a repeatable test. That topic scripts a first check. This one teaches the Java tool that many teams use to run checks.
Sources & standards — 6 sources
- Implementation-specific
JUnit 5.14.4 overview ↗ — JUnit team · Platform/Jupiter/Vintage roles; supported Java versions; selected release, not latest (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 dependency metadata ↗ — JUnit team · Version alignment and BOM; Platform1.14.4 standalone; Jupiter5.14.4 aggregator/API/params/engine (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 annotations ↗ — JUnit team · Jupiter test and lifecycle annotation roles (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 assertions ↗ — JUnit team · Expected/actual equality and assertion behavior (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 exception handling ↗ — JUnit team · Unexpected exceptions; throws clause versus an exception oracle; subtype versus exact assertions (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
Maven Surefire 3.6.1 JUnit Platform guide ↗ — Apache Software Foundation (Maven Surefire) · Platform/engine configuration and plugin role; no local Maven integration claim (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
PRACTISE AND KEEP
Sources for this topic — 6 sources
- Implementation-specific
JUnit 5.14.4 overview ↗ — JUnit team · Platform/Jupiter/Vintage roles; supported Java versions; selected release, not latest (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 dependency metadata ↗ — JUnit team · Version alignment and BOM; Platform1.14.4 standalone; Jupiter5.14.4 aggregator/API/params/engine (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 annotations ↗ — JUnit team · Jupiter test and lifecycle annotation roles (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 assertions ↗ — JUnit team · Expected/actual equality and assertion behavior (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
JUnit 5.14.4 exception handling ↗ — JUnit team · Unexpected exceptions; throws clause versus an exception oracle; subtype versus exact assertions (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
- Implementation-specific
Maven Surefire 3.6.1 JUnit Platform guide ↗ — Apache Software Foundation (Maven Surefire) · Platform/engine configuration and plugin role; no local Maven integration claim (selected actual primary passages read 7 November 2026; named JUnit5.14.4 or Surefire3.6.1 scope, not latest-version certification).
Actual selected primary sections read. The active guide header selects5.14.4; version-picker entries and newer-version notice are not its version. CSV introductory prose discrepancy is explained against attribute semantics/default and concrete false example. Synthetic helper/data examples do not claim application validation. Isolated runtime tests, if reported, do not certify Maven or every JVM.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.