Close

JUnit 5 - Assertions Class

[Last Updated: Dec 1, 2025]

JUnit 5 provides the Assertions class as the main class for performing test assertions.
This class has public static assertion methods.
Assertions allow us to verify expected outcomes in unit tests. This class replaces the older Assert class from JUnit 4 and introduces a more flexible and expressive set of methods.

JUnit Assertions Quick Methods Reference

Equality Assertions

public static void assertEquals(Object expected, Object actual)
public static void assertNotEquals(Object expected, Object actual)

public static void assertEquals(byte expected, byte actual)
public static void assertNotEquals(byte unexpected, byte actual)

Checks that expected and actual are equal.
Overloaded methods of assertEquals() exists for other primitive types (short, char, int, long, float, double) as well, this is to avoid autoboxing and hence making these method calls more efficient.


Boolean Assertions

public static void assertTrue(boolean condition)
public static void assertFalse(boolean condition)

Checks that the condition is true


Null Assertions

public static void assertNull(Object actual)
public static void assertNotNull(Object actual)

Checks that the object is null


Identity Assertions

public static void assertSame(Object expected, Object actual)
public static void assertNotSame(Object unexpected, Object actual)

Checks that expected and actual refer to the same object


Collection Assertions

public static void assertArrayEquals(byte[] expected, byte[] actual)
public static void assertIterableEquals(Iterable<?> expected, Iterable<?> actual)
public static void assertLinesMatch(List<String> expected, List<String> actual)

Checks that expected and actual arrays are equal


Exception Assertions

public static void assertThrows(Class<? extends Throwable> expectedType, Executable executable)
public static void assertDoesNotThrow(Executable executable)

Checks that executable throws the expected exception


Group Assertions

public static void assertAll(String heading, Executable... executables)

Groups multiple assertions and reports all failures


Failure Assertions

public static void fail(String message)

Explicitly fails the test with the given message


Type Assertions

public static void assertInstanceOf(Class<?> expectedType, Object actual)

Checks that actual is an instance of expectedType


Timeout Assertions

public static void assertTimeout(Duration timeout, Executable executable)
public static void assertTimeoutPreemptively(Duration timeout, Executable executable)

Checks that executable completes within the given timeout

Overloaded Methods Variants

Most assertion methods come with several overloads. Typically, the overloads include versions that accept:

  1. basic parameters: expected and actual.
  2. basic parameters with failure message as String
  3. basic parameters with a failure message as Supplier that allows for lazy evaluation of the message content.
  4. basic floating parameters with or without message and delta, which specifies the acceptable tolerance range for approximate equality for floating-point

Following are some of the overloaded variants:

public static void assertEquals(Object expected, Object actual) 

public static void assertEquals(Object expected, Object actual, String message)

public static void assertEquals(Object expected, Object actual, Supplier<String> messageSupplier)

public static void assertEquals(double expected, double actual, double delta)

public static void assertEquals(double expected, double actual, double delta, String message)

public static void assertEquals(double expected, double actual, double delta, Supplier<String> messageSupplier) 


In the next tutorials we will see the examples of Assertions methods.

See Also