An assertion in JUnit is a check that compares an expected result with the actual result of the code under test. If the check holds, the test continues; if it does not, JUnit throws an AssertionFailedError and marks the test as failed. In JUnit 5 and later, all the built-in assertions are static methods on org.junit.jupiter.api.Assertions.
Assertions are what turn a method that merely runs code into a real test. Without them a test can only fail by throwing an unexpected exception.
A test method calls the code under test, then passes the result to one or more assertion methods. The first argument is normally the expected value and the second is the actual value. The last argument can be an optional failure message.
When an assertion fails, the exception carries the expected and actual values, so the IDE or build tool can show a diff. The remaining statements in that test method are not executed, which is why a single test should check one behaviour.
The same Assertions API is used by JUnit 5 (Jupiter) and its successors, so the examples below apply to any current Jupiter-based setup. JUnit 4 used org.junit.Assert, where the optional message came first; Jupiter moved it to the last argument.
The table lists the assertions you will use most often.
| Method | What it checks |
|---|---|
assertEquals(expected, actual) | Both values are equal (equals() for objects) |
assertNotEquals(unexpected, actual) | The values are different |
assertTrue(condition) / assertFalse(condition) | A boolean condition holds or does not hold |
assertNull(value) / assertNotNull(value) | A reference is null or not |
assertSame(expected, actual) / assertNotSame(...) | Two references point to the same object (==) |
assertArrayEquals(expected, actual) | Two arrays have equal contents |
assertIterableEquals(expected, actual) | Two iterables return equal elements in the same order |
assertLinesMatch(expected, actual) | Lists of strings match, with regular expression support |
assertInstanceOf(Type.class, value) | A value is of a given type (since JUnit 5.8) |
assertThrows(Type.class, executable) | Code throws the expected exception |
assertDoesNotThrow(executable) | Code completes without an exception |
assertTimeout(duration, executable) | Code finishes within a time limit |
assertAll(executables...) | Several assertions are all evaluated and reported together |
fail(message) | Fails the test unconditionally |
Use assertEquals for value comparison and assertSame only when object identity matters. Comparing two String objects with assertSame is a common mistake, because equal strings are not guaranteed to be the same instance.
Floating-point arithmetic is not exact, so pass a tolerance as the third argument.
Without the delta, assertEquals(0.3, 0.1 + 0.2) fails because the sum is 0.30000000000000004.
assertThrows runs a lambda and checks that it throws an exception of the given type (or a subclass). It returns the exception, so you can inspect the message.
Use assertThrowsExactly when a subclass of the expected exception should not pass. Keep the lambda small, so that only the line you are testing can throw.
Normally the first failed assertion stops the test. assertAll evaluates every assertion in the group and reports all failures together, which is useful when checking several properties of one object.
If two of the three checks fail, the report lists both, so you fix them in one run instead of two.
assertTimeout waits for the code to finish and then fails if it took too long. assertTimeoutPreemptively runs the code in a different thread and aborts it when the limit is reached.
Passing a Supplier<String> as the message means the string is only built when the assertion fails. Prefer assertTimeoutPreemptively with care: because it uses another thread, thread-local state such as a Spring transaction is not shared with it.
The built-in assertions are enough for most tests. For readable checks on collections, strings and objects, many teams add AssertJ or Hamcrest, which offer fluent chains and richer failure messages.
You can mix both styles in one project. Pick one style per codebase for consistency.
assertEquals compares values with equals(), while assertSame checks that both references point to the exact same object. Use assertEquals unless identity is the thing you are testing.
The failure message reads "expected: X but was: Y". If you swap the arguments, the message is misleading and debugging takes longer.
Use assertThrows(ExceptionType.class, () -> codeUnderTest()). In JUnit 4 you would have used @Test(expected = ...), which does not exist in Jupiter.
Yes, when they verify one behaviour. Wrap related checks in assertAll so every failure is reported, and write a separate test for a separate behaviour.
Assertions compare expected and actual results and fail the test when they differ. Use assertEquals, assertTrue and assertNotNull for everyday checks, assertThrows for exceptions, assertAll to report several failures at once, and a delta for floating-point values. Add a custom message supplier when the default failure text is not clear enough.