Video in progress

Junior 🧪 Testing

When is assertAll better than ordinary sequential asserts?

Ordinary asserts stop at the first failure. assertAll runs every check and shows all the failures together, which is handy when checking several fields of one object.

📖 Answer

@Test
void foydalanuvchi_dto_togri_toldiriladi() {
    UserDto dto = mapper.toDto(user);

    assertAll("dto",
        () -> assertEquals("Aziz", dto.firstName()),
        () -> assertEquals("Karimov", dto.lastName()),
        () -> assertEquals("aziz@mail.uz", dto.email())
    );
}

If all three fields in the mapper are wrong, with ordinary asserts you fix the first, rerun the test, then see the second... assertAll reports all three at once.

When you don't need it: when the checks depend on each other. For example, first assertNotNull(user), then user.getName(): if the first fails, the second throws an NPE. In that case leave the first one outside assertAll.

In AssertJ the equivalent is called SoftAssertions.