JUnit 5 assertions are static methods in org.junit.jupiter.api.Assertions that fail a test when a condition is false, and assertAll groups several of them so every failure is reported instead of only the first. Using the right assertion and keeping the expected value first gives clear failure messages. This guide covers junit 5 assertions assertall with examples you can paste into a test class.
All examples assume import static org.junit.jupiter.api.Assertions.*; or the individual static imports.
The argument order is assertEquals(expected, actual). If you swap them, the test still passes or fails correctly, but the failure message names the wrong value as expected, which confuses the reader.
For double and float, pass a tolerance, because floating-point results are rarely exact:
assertSame and assertNotSame compare object identity (==), while assertEquals uses equals. For arrays and iterables:
assertLinesMatch treats each expected line as a regular expression first, which helps with log or report output.
Every assertion accepts a message as its last argument. Pass a Supplier<String> when building the message is expensive, because it is only evaluated if the assertion fails:
Prefer a message that says what should be true, not "test failed".
assertThrows returns the exception, so you can check its message:
assertThrows accepts subclasses of the given type; use assertThrowsExactly when the exact class matters. To check speed, use assertTimeout(Duration.ofMillis(200), () -> ...). It runs the code in the calling thread and fails after completion if it was too slow. assertTimeoutPreemptively runs it in another thread and aborts it, which can break code that relies on thread-local state, such as transactions.
Plain assertions stop at the first failure. When you validate several properties of one result, assertAll runs every executable and reports all failures together:
If two checks fail, the report lists both:
The first argument is a heading for the group. Lambdas in assertAll can throw checked exceptions, because they are Executable instances. Use assertAll for properties of one object or one action, not to cram unrelated scenarios into a single test.
The built-in assertions are enough for many tests. Libraries such as AssertJ add fluent, more readable assertions (assertThat(list).hasSize(2).contains("a")), and Hamcrest matchers are also supported. You can use them next to JUnit 5, since JUnit only needs an AssertionError to be thrown.
assertEquals compares values with equals, while assertSame checks that both references point to the same object.
No. It runs all the executables and throws one MultipleFailuresError listing every failure.
Prefer assertEquals. assertTrue(a == b) only says "expected true", while assertEquals(a, b) shows both values.
Use assertDoesNotThrow. Without it, an unexpected exception already fails the test, so use it mainly to make the intent clear.
JUnit 5 provides assertEquals, assertTrue, assertNull, assertArrayEquals, assertThrows and time-limit assertions in Assertions. Put the expected value first, add helpful messages, and use assertAll to see all failures of one result at once. Add AssertJ when you want fluent assertions.