JUnit Jupiter API for writing tests - Core API module of JUnit 5 that provides annotations, assertions, and test lifecycle management
npx @tessl/cli install tessl/maven-org-junit-jupiter--junit-jupiter-api@5.12.0JUnit Jupiter API is the core testing framework for Java applications, providing comprehensive annotations, assertions, and test lifecycle management. It offers modern testing capabilities including conditional execution, parameterized tests, parallel execution, and an extensible architecture through its Extension Model.
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.12.2</version></dependency>import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;For specific functionality:
// Core test annotations
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
// Assertions and assumptions
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
// Conditional execution
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.DisabledOnOs;
// Extensions
import org.junit.jupiter.api.extension.ExtendWith;import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
private Calculator calculator;
@BeforeEach
void setUp() {
calculator = new Calculator();
}
@Test
@DisplayName("Addition of two positive numbers")
void testAddition() {
int result = calculator.add(2, 3);
assertEquals(5, result, "2 + 3 should equal 5");
}
@Test
void testDivisionByZero() {
assertThrows(ArithmeticException.class, () -> {
calculator.divide(10, 0);
}, "Division by zero should throw ArithmeticException");
}
@AfterEach
void tearDown() {
calculator = null;
}
}JUnit Jupiter API is built around several key components:
Essential testing annotations, assertions, and lifecycle management for writing Java tests. Provides the foundation for all JUnit Jupiter testing scenarios.
// Core test lifecycle annotations
@Test
@BeforeEach
@AfterEach
@BeforeAll
@AfterAll
// Test organization and metadata
@DisplayName(String value)
@Disabled(String value)
@Tag(String value)
@Nestedimport java.time.Duration;
import org.junit.jupiter.api.function.Executable;
// Core assertions (static methods in Assertions class)
static void assertTrue(boolean condition);
static void assertTrue(boolean condition, String message);
static void assertFalse(boolean condition);
static void assertEquals(Object expected, Object actual);
static void assertNotEquals(Object unexpected, Object actual);
static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable);
static void assertTimeout(Duration timeout, Executable executable);
static void assertAll(Executable... executables);Runtime environment-based test control for platform-specific, JRE-specific, and custom conditional testing scenarios.
// Operating system conditions
@EnabledOnOs(OS.LINUX)
@DisabledOnOs({OS.WINDOWS, OS.MAC})
// JRE version conditions
@EnabledOnJre(JRE.JAVA_11)
@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_17)
// System properties and environment variables
@EnabledIfSystemProperty(named = "os.arch", matches = ".*64.*")
@EnabledIfEnvironmentVariable(named = "ENV", matches = "ci")
// Custom method conditions
@EnabledIf("customCondition")
@DisabledIf("customCondition")Comprehensive extension system for custom test behavior, dependency injection, lifecycle callbacks, and test monitoring.
// Extension registration
@ExtendWith(MyExtension.class)
@RegisterExtension
static final MyExtension extension = new MyExtension();
// Core extension interfaces
interface Extension { }
interface BeforeAllCallback extends Extension {
void beforeAll(ExtensionContext context);
}
interface ParameterResolver extends Extension {
boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext);
Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext);
}
// Extension context
interface ExtensionContext {
Optional<ExtensionContext> getParent();
String getDisplayName();
Set<String> getTags();
Store getStore(Namespace namespace);
}Thread-safe test execution with resource management, temporary directory support, and execution control annotations.
// Parallel execution control
@Execution(ExecutionMode.CONCURRENT)
@Execution(ExecutionMode.SAME_THREAD)
@Isolated
// Resource management
@ResourceLock(value = "shared.resource", mode = ResourceAccessMode.READ)
@ResourceLock(value = "exclusive.resource", mode = ResourceAccessMode.READ_WRITE)
// Temporary directory support
@TempDir
Path tempDirectory;
@TempDir(cleanup = CleanupMode.ALWAYS)
File tempDir;Parallel Execution and I/O Support
interface TestInfo {
String getDisplayName();
Set<String> getTags();
Optional<Class<?>> getTestClass();
Optional<Method> getTestMethod();
}
interface TestReporter {
void publishEntry(Map<String, String> map);
void publishEntry(String key, String value);
}
interface RepetitionInfo {
int getCurrentRepetition();
int getTotalRepetitions();
}@FunctionalInterface
interface Executable {
void execute() throws Throwable;
}
@FunctionalInterface
interface ThrowingSupplier<T> {
T get() throws Throwable;
}
@FunctionalInterface
interface ThrowingConsumer<T> {
void accept(T t) throws Throwable;
}enum TestInstance.Lifecycle {
PER_METHOD, PER_CLASS
}
enum OS {
AIX, FREEBSD, LINUX, MAC, OPENBSD, SOLARIS, WINDOWS, OTHER;
static OS current();
boolean isCurrentOs();
}
enum ExecutionMode {
SAME_THREAD, CONCURRENT
}
enum ResourceAccessMode {
READ, READ_WRITE
}
enum CleanupMode {
DEFAULT, ALWAYS, ON_SUCCESS, NEVER
}