Assertions are the heart of every JUnit 6 test. They are the statements that determine whether a test passes or fails. JUnit 6 provides a rich set of assertion methods in the org.junit.jupiter.api.Assertions class β from simple equality checks to grouped assertions, exception verification, and timeout enforcement. This guide covers every assertion method with real examples, console output, and guidance on when to use each one.
All assertion methods are static, so the standard pattern is to use a static import at the top of your test class:
import static org.junit.jupiter.api.Assertions.*;
1. assertEquals and assertNotEquals
The most commonly used assertions. They compare two values for equality using .equals():
@Test
@DisplayName("assertEquals and assertNotEquals examples")
void equalityAssertions() {
String greeting = "Hello, JUnit 6";
// Basic equality check
assertEquals("Hello, JUnit 6", greeting);
// With a custom failure message (shown if assertion fails)
assertEquals("Hello, JUnit 6", greeting, "Greeting string should match exactly");
// Lazy message: the Supplier lambda only evaluates if the assertion FAILS
// Use this when building the message is expensive (e.g. involves I/O)
assertEquals("Hello, JUnit 6", greeting,
() -> "Expected greeting to be 'Hello, JUnit 6' but got: " + greeting);
// assertNotEquals: passes when values are NOT equal
assertNotEquals("Goodbye", greeting, "Greeting should not be 'Goodbye'");
// Floating-point comparison with a delta (tolerance)
// Never compare doubles with == or assertEquals without a delta
double result = 0.1 + 0.2; // = 0.30000000000000004 due to floating-point
assertEquals(0.3, result, 0.0001, "0.1 + 0.2 should be approximately 0.3");
}
2. assertTrue and assertFalse
@Test
@DisplayName("assertTrue and assertFalse examples")
void booleanAssertions() {
List languages = List.of("Java", "Python", "Go");
// assertTrue: passes when condition is true
assertTrue(languages.contains("Java"), "List should contain Java");
assertTrue(languages.size() > 0, "List should not be empty");
// assertFalse: passes when condition is false
assertFalse(languages.isEmpty(), "List should not be empty");
assertFalse(languages.contains("COBOL"), "List should not contain COBOL");
// Works with Predicate-style lambdas for complex conditions
assertTrue(languages.stream().anyMatch(lang -> lang.startsWith("J")),
"At least one language should start with J");
}
3. assertNull and assertNotNull
@Test
@DisplayName("assertNull and assertNotNull examples")
void nullAssertions() {
String uninitialised = null;
String initialised = "JUnit 6";
// assertNull: passes when value IS null
assertNull(uninitialised, "Uninitialised variable should be null");
// assertNotNull: passes when value is NOT null
assertNotNull(initialised, "Initialised variable should not be null");
// Common real-world usage: verify a factory method returns a non-null object
Order order = OrderFactory.createDefault();
assertNotNull(order, "OrderFactory.createDefault() must not return null");
assertNotNull(order.getId(), "Created order must have an assigned ID");
}
