JUnit 5
Prev: Gradle | Up: third_party/java | Next: Spring Framework
Rel: Apache Maven, Gradle, Spring Framework
Ref: JUnit 5 User Guide
Tags: #public #prog #java #third-party #testing #tier/core
Overview
JUnit 5 is the standard testing framework for modern Java. It is split into three sub-projects:
- JUnit Platform: Foundation for launching test frameworks on the JVM (used by IDEs, Maven Surefire, and Gradle).
- JUnit Jupiter: The modern programming & extension model (
org.junit.jupiter.api.*). - JUnit Vintage: Optional engine for running
#tier/legacyJUnit 3/4 tests during migration.
Core Annotations & Assertions
Test classes and methods in JUnit 5 do not need to be public (package-private is idiomatic):
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class PriceCalculatorTest {
private PriceCalculator calculator;
@BeforeEach
void setUp() {
calculator = new PriceCalculator();
}
@Test
@DisplayName("Applies percentage discount and rounds half-up")
void appliesDiscount() {
int result = calculator.applyDiscount(1000, 15);
assertEquals(850, result);
}
@Test
void rejectsNegativePrice() {
var ex = assertThrows(IllegalArgumentException.class,
() -> calculator.applyDiscount(-1, 10));
assertTrue(ex.getMessage().contains("negative"));
}
@ParameterizedTest
@CsvSource({
"100, 0, 100",
"100, 50, 50",
"200, 100, 0"
})
void discountTable(int price, int pct, int expected) {
assertEquals(expected, calculator.applyDiscount(price, pct));
}
@Test
void groupedAssertionsAndTempDir(@TempDir Path tempDir) throws Exception {
Path file = tempDir.resolve("receipt.txt");
Files.writeString(file, "total=850");
assertAll(
() -> assertTrue(Files.exists(file)),
() -> assertEquals("total=850", Files.readString(file))
);
}
@Nested
class WhenCartIsEmpty {
@Test
void totalIsZero() {
assertEquals(0, calculator.totalOf());
}
}
}
Lifecycle & Extension Highlights
- Lifecycle hooks:
@BeforeAll/@AfterAll(static by default) and@BeforeEach/@AfterEach(new test instance per method by default, isolating state). @TempDir: Built-in extension that provisions and cleans up temporary directories per test.assertAll(...): Executes every assertion in the group and reports all failures together rather than stopping at the first failure.- Custom extensions: Replace JUnit 4
@RunWith/@Rulewith composable@ExtendWith(...)implementations (BeforeEachCallback,ParameterResolver, etc.).
Linked from
Related notes
- tuts/third_party/java/gradle Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java/maven Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java/spring_framework Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java Linked here; links to this page; shared tags: #java, #prog
- tuts/third_party/java/spring_boot links to this page; shared tags: #java, #prog, #tier
- tuts/java links to this page; shared tags: #java, #prog
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #prog, #tier
- tuts/java/builtins/AutoCloseable shared tags: #java, #prog, #tier
- tuts/java/builtins/enum shared tags: #java, #prog, #tier
- tuts/java/builtins/final shared tags: #java, #prog, #tier