
The JUnit 5 Handbook for Readable Fast Reliable Tests
A working reference to JUnit 5 and the JUnit 6 line: lifecycle, assertions, parameterized tests, extensions and the habits that keep suites fast.
Tag
18 posts

A working reference to JUnit 5 and the JUnit 6 line: lifecycle, assertions, parameterized tests, extensions and the habits that keep suites fast.

Decide what to unit test, what to run against real infrastructure and what to mock, with a layered plan that keeps a Spring Boot suite fast.

JUnit 5 is a Platform, an engine called Jupiter and a Vintage engine for old tests. See how they fit together and why it explains most setup errors.

Add JUnit 5 to a Maven or Gradle project, write a first test, run it from the command line and read the output, in about fifteen minutes.

See when BeforeAll, BeforeEach, AfterEach and AfterAll run, how nested classes and instance lifecycles change that order and how to control test order.

Use assertThrows to check the exception type and message, and assertAll to see every failure at once, with the mistakes that make both pass by accident.

Write assertions that say what they check and fail with a message you can act on, using JUnit 5 assertions and AssertJ's fluent style together.

Fix a Maven or Gradle build that reports zero tests by checking the Surefire version, test class names, imports and the JUnit Platform setting.

Fix the JUnit "No runnable methods" and initializationError failures caused by mixing JUnit 4 and JUnit 5 annotations or a wrongly configured runner.

Split fast and slow tests with JUnit Tag annotations, then select them in Maven Surefire and Failsafe or Gradle with tag expressions for CI stages.

Write parameterized tests in JUnit 5 with ParameterizedTest, ValueSource, NullSource, EnumSource and a display name, and know when a table is clearer.

Run JUnit tests only when a condition holds: EnabledOnOs, EnabledForJreRange, EnabledIfEnvironmentVariable, EnabledIf and custom ExecutionCondition.