Open In App

assertAll() vs Multiple Assertions in JUnit5

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

JUnit is one of the most popular unit testing frameworks within the Java environment. With many new features introduced in JUnit 5 compared to JUnit 4, it provides a more flexible and powerful testing framework. In this article, we will explore two key methods for asserting conditions in JUnit 5 that are assertAll() and multiple individual assertions.

assertAll()

assertAll() is a method that allows grouping multiple assertions into one test block. Unlike traditional assertions that stop the test execution as soon as a failure occurs, assertAll() collects all the failures and reports them at the end of the test execution. This is especially useful when you want to check multiple conditions at once and get feedback on all potential failures, not just the first one.

Example of assertAll()

Java
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class AssertAllExampleTest {

    @Test
    void testMultipleAssertionsWithAssertAll() {
        // Group multiple assertions using assertAll
        assertAll("Group of Assertions",
            () -> assertEquals(5, 5, "First assertion should pass"),  // Pass
            () -> assertTrue(10 > 1, "Second assertion should pass"), // Pass
            () -> assertFalse(2 > 5, "Third assertion should pass"),  // Pass
            () -> assertNotNull(null, "Fourth assertion should fail") // Fail
        );
    }
}

Explanation:

  • In this test, multiple assertions are grouped using assertAll().
  • Even though the assertNotNull(null) will fail, the test does not terminate immediately. Instead, JUnit will continue to execute the remaining assertions and report all failures at the end.
  • In this case, you will see three passing assertions and one failure in the test output.

Multiple Assertions

Multiple assertions in JUnit 5 work the same way they did in earlier versions of JUnit. Each assertion is evaluated independently, and if one assertion fails, the test is immediately terminated. This approach gives you instant feedback on the first failure but prevents subsequent assertions from running.

Example of Multiple Assertions

Java
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class MultipleAssertionsExampleTest {

    @Test
    void testMultipleIndividualAssertions() {
        // Each assertion is independent and runs sequentially
        assertEquals(5, 5, "First assertion should pass");   // Pass
        assertTrue(10 > 1, "Second assertion should pass");  // Pass
        assertFalse(2 > 5, "Third assertion should pass");   // Pass
        assertNotNull(null, "Fourth assertion should fail"); // Fail - Test stops here
        assertEquals(3, 3, "Fifth assertion won't be executed due to previous failure");
    }
}

Explanation:

  • In this test, each assertion is executed sequentially.
  • The test will stop executing after the first failure, which occurs at assertNotNull(null).
  • The last assertion (assertEquals(3, 3)) will not be executed because the test halts at the first failure.
  • JUnit will only report the first failure.

Difference between assertAll() and Multiple Assertions

assertAll()

Multiple Assertions

assertAll() ensures that all assertions are executed, even if some of them fail.

Multiple assertions stop the test as soon as the first assertion fails.

Reports all failures at the end, giving comprehensive feedback.

Only reports the first failure, which may not provide a complete picture of the test's state.

Each assertion inside assertAll() is evaluated using lambda expressions.

Each assertion is independent and runs sequentially.

Ideal for scenarios where multiple conditions need to be validated (e.g., validating multiple fields of an object).

Suitable for cases where you want to stop the test as soon as a failure occurs.

Results in a cleaner test when validating multiple assertions in a single method.

Requires multiple separate assertions, which can make the test longer and harder to read.

Conclusion

In this article, we have compared assertAll() and multiple assertions in JUnit 5. The key advantage of assertAll() is that it allows you to run and report multiple assertions at once, providing feedback on all failures in a single test case. This is useful when you need to validate several conditions at once. On the other hand, multiple individual assertions are more appropriate when you want the test to fail immediately on the first failure, providing quick feedback.

When writing tests, consider whether you need comprehensive feedback on all conditions or if it is preferable to stop execution as soon as the first failure occurs. Both approaches have their use cases and can be applied depending on the testing scenario.


Article Tags :

Similar Reads