Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What does assertAll do, and when is it better than several separate assertions?

assertAll takes a group of assertions, runs every one of them even when some fail, and then reports all the failures together in one MultipleFailuresError. Separate assertions stop at the first failure, so the later problems stay hidden until we fix the first one and run the test again. That makes assertAll the better choice when we check several independent fields of one result, such as the properties of a mapped object.

Why this question is asked

A test that fails with one message, we fix it, run again, and it fails with the next message, wastes a surprising amount of time, especially when the test takes a minute because it starts a Spring context or a container. Interviewers ask about assertAll because it shows whether we think about how tests fail, not only how they pass. In this answer, we will see why separate assertions hide failures, how assertAll changes the report, when grouping is the right choice and when it is not, and how message suppliers keep assertions cheap.

1Why Separate Assertions Hide Failures

Every JUnit assertion works by throwing an exception, an AssertionFailedError, when the check fails. An exception ends the method immediately, so any assertion after the failing one never runs. Here is a mapper test where two fields are wrong.

Java
@Test void mapsCustomer() {
CustomerDto dto = mapper.toDto(customer);
assertEquals("Ada", dto.firstName()); // fails: expected <Ada> but was <ada>
assertEquals("Lovelace", dto.lastName()); // never runs
assertEquals("GB", dto.country()); // never runs, also wrong: "UK"
}

The report shows only the first name problem. We fix it, run the test again, and only then discover that the country is wrong too. With two wrong fields that is two round trips; with a slow integration test it can be several minutes lost each time.

2Grouping the Checks With assertAll

assertAll accepts a heading and any number of executables, small lambdas that each contain one assertion. It runs every executable, collects the failures instead of stopping, and at the end throws a single MultipleFailuresError that lists them all.

Java
@Test void mapsCustomer() {
CustomerDto dto = mapper.toDto(customer);
assertAll("customer dto",
() -> assertEquals("Ada", dto.firstName()), // fails, recorded
() -> assertEquals("Lovelace", dto.lastName()), // passes
() -> assertEquals("GB", dto.country()) // fails, recorded
);
}
// org.opentest4j.MultipleFailuresError: customer dto (2 failures)
// expected: <Ada> but was: <ada>
// expected: <GB> but was: <UK>
Step throughSeparate assertions against assertAll
TestassertEqualsassertAll
check firstName
throws: test ends here
run three grouped checks
firstName fails, recorded
lastName passes
country fails, recorded
throws MultipleFailuresError with 2 failures

7 messages in order. Play them, or step through them one at a time.

Now one run tells us everything that is wrong with the mapping. Notice that the test still fails, so assertAll is not more lenient; it only reports more. The heading also helps when a test has several groups, because it says which group the failures belong to.

3When Grouping Helps and When It Does Not

assertAll is the right tool when the checks are independent: several fields of one result object, several headers of an HTTP response, or several values of a computed report. Each check means something on its own, so seeing all failures together gives a full picture.

It is the wrong tool when later checks depend on earlier ones. If the first check is that a result is not null and the second reads a field of it, running the second after the first failed only adds a confusing NullPointerException to the report. JUnit handles this in a neat way: inside an executable, assertions run normally and stop that executable, so we can put a dependent check first and the rest inside a nested assertAll.

Java
assertAll("order",
() -> {
assertNotNull(order.customer()); // if this fails, the next line is skipped
assertEquals("Ada", order.customer().name()); // only runs when customer exists
},
() -> assertEquals(3, order.lines().size()),
() -> assertEquals(new BigDecimal("42.50"), order.total())
);

So we group independent checks and keep dependent ones in sequence. We also should not wrap every assertion of every test in assertAll out of habit. A test that checks one behaviour usually needs one or two assertions, and many grouped assertions can be a sign that the test covers too much.

4Cheap Messages With Message Suppliers

Every assertion accepts an optional failure message as its last argument. If we pass a String, Java builds it before calling the assertion, even when the assertion passes. With string concatenation or a toString() of a large object, that cost is paid on every run, in loops possibly thousands of times. The alternative is a Supplier<String>, a lambda that JUnit calls only when the assertion fails.

Java
// The message is built every time, even when the assertion passes
assertTrue(isValid(row), "invalid row: " + row.toDebugString());
// The lambda runs only on failure
assertTrue(isValid(row), () -> "invalid row: " + row.toDebugString());

The difference is small for a single check but real when the message is expensive or the assertion runs inside a loop over many rows. Messages also matter inside assertAll, because a list of five failures is much easier to read when each one says what it checked.

5Follow-up questions

What exception does assertAll throw?

It throws org.opentest4j.MultipleFailuresError, which contains every failure from the group, each with its own message. If an executable throws an exception that is not an assertion error, assertAll collects it too, unless it is one of the serious errors that should never be swallowed, such as OutOfMemoryError, which is rethrown immediately.

How does this compare with AssertJ's soft assertions?

AssertJ's SoftAssertions do the same job in a fluent style: we record many assertions and they are all checked at the end with assertAll() on the soft assertions object, or automatically with its JUnit extension. The idea is the same, report every failure in one run; the choice is mostly about which assertion library a team uses.

Can assertAll take a collection of checks?

Yes. Besides varargs, it accepts a Collection or a Stream of Executable, which is handy when we build the checks from data, for example one check per expected field in a map.

6Conclusion

In this answer, we saw that ordinary assertions throw on the first failure and hide the rest, which costs extra test runs. assertAll runs every grouped check and reports all failures together in a MultipleFailuresError, which suits independent checks on several fields of one result. Dependent checks can be placed inside one executable or nested groups, and message suppliers avoid building failure messages when assertions pass.

What to say in the interviewAbout 80 seconds aloud

Open with

assertAll groups several assertions and runs all of them, even when some fail, and at the end throws one MultipleFailuresError that lists every failure. Normal assertions throw on the first failure, so the method stops and the later checks never run.

Then, if they want more

If a mapper gets two fields wrong, separate assertions show me only the first one, I fix it, rerun, and only then see the second, which is painful when the test starts a Spring context. With assertAll I see both in one run.

I use it for independent checks, like several fields of a DTO or several headers of a response. When checks depend on each other, like not-null and then reading a field, I keep them in sequence inside one executable, so the second is skipped if the first fails, and I can nest assertAll for that.

I also use message suppliers, a lambda instead of a String, for failure messages that are expensive to build, because the lambda only runs when the assertion fails. And I would not wrap everything in assertAll by habit; many assertions in one test can mean the test is covering too much.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JUnit user guide
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does assertAll do, and when is it better than several separate assertions?": https://scrivio-interview-library.netlify.app/java/junit/002880e174/