assertAll

A test that checks five parts of an object with five assertions stops at the first wrong one. The person fixes it, runs again, and finds the second wrong one. assertAll runs every assertion first and reports all the failures together, which turns three runs into one. This lesson uses it on a record with two wrong values.

The record and one test

Profile.java is a record with a name, an age and a city. ProfileTest.java builds one profile and checks all three components in one assertAll. Two of the three expected values are wrong on purpose: the age is 36 but the test wants 37, and the city is London but the test wants Paris.

public record Profile(String name, int age, String city) { }

import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class ProfileTest { @Test void everyPartOfTheProfile() { Profile profile = new Profile("Ada", 36, "London"); assertAll( () -> assertEquals("Ada", profile.name()), () -> assertEquals(37, profile.age()), () -> assertEquals("Paris", profile.city())); } }

The report with two failures

javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out Profile.java ProfileTest.java
junit execute --class-path out --scan-classpath --disable-ansi-colors


Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│  └─ ProfileTest ✔
│     └─ everyPartOfTheProfile() ✘ Multiple Failures (2 failures)
│               org.opentest4j.AssertionFailedError: expected: <37> but was: <36>
│               org.opentest4j.AssertionFailedError: expected: <Paris> but was: <London>
└─ JUnit Vintage ✔

Failures (1):
  JUnit Jupiter:ProfileTest:everyPartOfTheProfile()
    MethodSource [className = 'ProfileTest', methodName = 'everyPartOfTheProfile', methodParameterTypes = '']
    => org.opentest4j.MultipleFailuresError: Multiple Failures (2 failures)
org.opentest4j.AssertionFailedError: expected: <37> but was: <36>
org.opentest4j.AssertionFailedError: expected: <Paris> but was: <London>
       org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:80)
       org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:44)
       org.junit.jupiter.api.AssertAll.assertAll(AssertAll.java:38)
       org.junit.jupiter.api.Assertions.assertAll(Assertions.java:2944)
       ProfileTest.everyPartOfTheProfile(ProfileTest.java:10)
       [...]
       Suppressed: org.opentest4j.AssertionFailedError: expected: <37> but was: <36>
         org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
         org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
         org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
         org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
         org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)
         [...]
       Suppressed: org.opentest4j.AssertionFailedError: expected: <Paris> but was: <London>
         org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
         org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
         org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
         org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
         org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
         [...]

Test run finished after 311 ms
[         4 containers found      ]
[         0 containers skipped    ]
[         4 containers started    ]
[         0 containers aborted    ]
[         4 containers successful ]
[         0 containers failed     ]
[         1 tests found           ]
[         0 tests skipped         ]
[         1 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         1 tests failed          ]

The tree line says Multiple Failures (2 failures) and the two lines under it name both wrong values. The failure list repeats them under one MultipleFailuresError, and each is a suppressed AssertionFailedError with its own stack. The counts table still says one test, because all of this happened inside one method.

What assertAll does

Each argument is an Executable, a lambda that returns nothing and may throw. assertAll calls them one after another, catches the failure of each, and throws one MultipleFailuresError at the end with every caught failure attached as a suppressed exception. The first failure does not stop the rest.

A heading can be the first argument: assertAll("profile", ...). The heading appears in the message, which helps when a class has several groups of checks. The lambdas can call any assertion, and a non-assertion exception is also collected.

Against separate assertions

Three separate assertEquals calls would report only the first wrong value, the age, because the second call never runs. The run would be red in both cases, so the difference is not the verdict; it is the number of runs needed to learn everything. One assertAll with independent checks is therefore the usual shape for a constructor test or a record test.

The rule has a limit: put checks that depend on each other outside the group. If the second call throws a NullPointerException because the first value was null, the report shows an error, and the group should have stopped before it. A guard assertion followed by the group is clearer than a group that hides a null.

The fix

Change 37 to 36 and Paris to London. The test then has one successful method, no failure list, and status 0. The point of the lesson is that the red run named both changes at once, so the fix was one edit rather than two runs.

Exercises

  1. Fix both values and run. Confirm that the failure list is gone and the status is 0.
  2. Add a fourth check for a component you add to the record. How many failures does the report list when two of the four are wrong?
  3. Replace assertAll with three separate assertions and run. Which failure is reported, and which one is missing?
  4. Add "profile" as the first argument and read the message in the failure list.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does assertAll do when the second of three checks fails?

    Answer

    It runs the third check and reports every failure at the end

    assertAll collects the failures and throws them together after every executable has run.

  2. What exception does assertAll throw?

    Answer

    org.opentest4j.MultipleFailuresError

    The multiple-failures error carries each assertion failure as a suppressed exception.

  3. When is a group of assertions the wrong choice?

    Answer

    When a later check depends on an earlier value being non-null

    A dependent check can throw an error that masks the missing value; guard it before the group.

  4. What does this program print? public class Q1 { public static void main(String[] args) { int age = 36; System.out.println(age + 1); } }

    Answer

    37

    The addition gives 37, and println prints it.

  5. What does this program print? public class Q2 { public static void main(String[] args) { String city = "London"; System.out.println(city.equals("London")); } }

    Answer

    true

    The two strings have the same characters, so equals is true.

  6. What does this program print? public class Q3 { record Profile(String name, int age) { } public static void main(String[] args) { Profile p = new Profile("Ada", 36); System.out.println(p.name() + " " + p.age()); } }

    Answer

    Ada 36

    The accessors return the components and the strings are joined with a space.

  7. What does this program print? public class Q4 { public static void main(String[] args) { int total = 0; for (int n = 1; n != 4; n++) { total += n; } System.out.println(total); } }

    Answer

    6

    The loop adds 1, 2 and 3, and stops when n is 4.

  8. How many tests does the counts table see for one method that contains three assertEquals calls inside assertAll?

    Answer

    One

    The counts follow test methods, not assertions; the three checks are one test.

  9. What does this program print? public class Q5 { public static void main(String[] args) { String name = "Ada"; System.out.println(name.length() + 33); } }

    Answer

    36

    length is 3, and 3 + 33 is 36.

  10. What is the heading argument of assertAll for?

    Answer

    It names the group in the failure message

    A string first argument labels the group of checks in the multiple-failures message.

The code on this page was built with OpenJDK 21.0.13 (javac) and run with JUnit Platform Console Launcher 1.14.3 on Alpine Linux in a Docker container.

Article author: Arthur Isaev