In JUnit 5, org.junit.jupiter.api.Assertions is a utility class of static methods that check whether your code produced the expected result. When an assertion fails it throws an AssertionFailedError, which marks the test as failed and reports the expected and actual values. It replaces the older org.junit.Assert class from JUnit 4.
You use it for equality, nulls, booleans, exceptions, collections, grouped checks and timeouts.
Import the methods statically and call them in a test:
The order is expected first, actual second: assertEquals(expected, actual). Reversing them still passes or fails correctly but makes failure messages misleading. In JUnit 5 the optional failure message is the last argument, and it can be a Supplier<String> so the text is built only when the test fails:
| Method | Checks |
|---|---|
assertEquals, assertNotEquals | Equality using equals; has overloads for primitives and a delta for floating point |
assertSame, assertNotSame | Reference identity |
assertTrue, assertFalse | Boolean conditions |
assertNull, assertNotNull | Null checks |
assertArrayEquals | Array contents |
assertIterableEquals | Element-by-element equality of iterables |
assertLinesMatch | Lists of lines, with regular expressions allowed |
assertInstanceOf | Type check that returns the cast value (JUnit 5.8+) |
assertThrows, assertDoesNotThrow | Exceptions |
assertTimeout, assertTimeoutPreemptively | Execution time |
assertAll | Several assertions at once |
fail | Fails the test explicitly |
For floating-point values, pass a tolerance: assertEquals(0.3, 0.1 + 0.2, 1e-9).
assertThrows runs a block, requires a specific exception type, and returns the exception so you can inspect it:
It also passes for subclasses of the expected type. Use assertDoesNotThrow when the point of the test is that nothing is thrown.
A normal test stops at the first failed assertion. assertAll runs every executable and reports all failures together:
This is useful for objects with many fields, because one run shows everything that is wrong.
assertTimeout waits for the code to finish and then fails if it took too long. assertTimeoutPreemptively runs the code in another thread and aborts it, which can break code that depends on thread-local state such as a transaction.
The built-in methods cover most needs. Libraries such as AssertJ offer fluent chains (assertThat(list).containsExactly(...)) with richer failure messages and are popular for collections and objects. They add to JUnit rather than replace it; the test is still run by JUnit and failures are still AssertionErrors.
Package and class are different (org.junit.Assert versus org.junit.jupiter.api.Assertions). In JUnit 5 the message is the last argument instead of the first, and it adds assertAll, assertThrows as a first-class method, and lambda support.
Technically no for pass or fail, but yes for readable failure messages. Always put the expected value first.
Use assertDoesNotThrow(() -> service.run()). It also returns the result when you use the ThrowingSupplier overload.
It reports every failed check in one run rather than stopping at the first, which saves repeated test runs.
The Assertions class is the core of verification in JUnit 5. Use assertEquals and friends with expected first, assertThrows for error paths, assertAll to group related checks, and assertTimeout for time limits. Add AssertJ if you want more expressive assertions.