JUnit is Java's most widely adopted testing framework, enabling developers to write and execute unit tests with annotations, assertions, and lifecycle management. JUnit 6 (released September 2025, current GA 6.0.3) requires Java 17 and unifies Platform + Jupiter + Vintage versioning — introducing @ParameterizedClass for whole-class parameterization, native Kotlin coroutine support, CancellationToken-based fail-fast execution, and JFR integration built into the launcher. Understanding JUnit's lifecycle hooks, assertion methods, and extension model lets practitioners write maintainable, isolated tests that catch regressions early and document expected behavior through executable specifications.
What This Cheat Sheet Covers
This topic spans 16 focused tables and 146 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core Test Annotations
| Annotation | Example | Description |
|---|---|---|
void testAddition() { ... } | • Marks a method as a test case to be executed by the JUnit runner • the foundational annotation for all test methods. | |
void setUp() { obj = new MyClass(); } | • Executes before each test method • ideal for initializing test fixtures or resetting state. | |
void tearDown() { obj.close(); } | • Executes after each test method • used for cleanup like closing resources or clearing state. | |
static void setUpAll() { db = new DB(); } | • Executes once before all tests • must be static unless @TestInstance(PER_CLASS) is used• for expensive setup. | |
static void tearDownAll() { db.shutdown(); } | • Executes once after all tests • must be static unless @TestInstance(PER_CLASS) is used• for final cleanup. |