How do I group multiple assertions with assertAll()?
In JUnit 5, you can group multiple assertions using assertAll(). This lets JUnit run all assertions in the group, even if one fails, and then report all failures together. Basic Syntax import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class UserTest { @Test void testUserDetails() { User user = new User("Alice", 25, "alice@example.com"); assertAll("User details", () -> assertEquals("Alice", […]
Artikel oleh Wayan · Sumber: Kode Java ↗
Terjemahan artikel ini belum tersedia. Isi asli ditampilkan.
In JUnit 5, you can group multiple assertions using assertAll(). This lets JUnit run all assertions in the group, even if one fails, and then report all failures together.
Basic Syntax
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class UserTest {
@Test
void testUserDetails() {
User user = new User("Alice", 25, "alice@example.com");
assertAll("User details",
() -> assertEquals("Alice", user.getName()),
() -> assertEquals(25, user.getAge()),
() -> assertEquals("alice@example.com", user.getEmail())
);
}
}
Each assertion is written as a lambda expression:
() -> assertEquals(expected, actual)
Why Use assertAll()?
Without assertAll(), JUnit stops at the first failed assertion:
assertEquals("Alice", user.getName());
assertEquals(25, user.getAge());
assertEquals("alice@example.com", user.getEmail());
If the first assertion fails, the remaining assertions are not executed.
With assertAll(), JUnit executes every assertion inside the group and reports all failures at once.
Example with Multiple Failures
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void testCalculatorResults() {
int result = 10;
assertAll("Calculator checks",
() -> assertEquals(10, result),
() -> assertTrue(result > 0),
() -> assertFalse(result < 0),
() -> assertNotEquals(5, result)
);
}
}
Nested assertAll()
You can also group assertions into nested sections:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PersonTest {
@Test
void testPerson() {
Person person = new Person("John", "Doe", 30);
assertAll("Person",
() -> assertAll("Name",
() -> assertEquals("John", person.getFirstName()),
() -> assertEquals("Doe", person.getLastName())
),
() -> assertAll("Age",
() -> assertEquals(30, person.getAge()),
() -> assertTrue(person.getAge() >= 18)
)
);
}
}
Using assertAll() with a List
You can use assertAll() when checking multiple objects too:
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class ProductTest {
@Test
void testProducts() {
List<String> products = List.of("Book", "Pen", "Notebook");
assertAll("Product list",
() -> assertEquals(3, products.size()),
() -> assertTrue(products.contains("Book")),
() -> assertTrue(products.contains("Pen")),
() -> assertTrue(products.contains("Notebook"))
);
}
}
Important Note
assertAll() is best used when assertions are independent of each other.
Good:
assertAll("User",
() -> assertEquals("Alice", user.getName()),
() -> assertEquals(25, user.getAge()),
() -> assertEquals("alice@example.com", user.getEmail())
);
Avoid placing dependent logic inside it:
assertAll("User",
() -> assertNotNull(user),
() -> assertEquals("Alice", user.getName())
);
If user is null, the second assertion may throw a NullPointerException. In that case, check assertNotNull(user) before assertAll():
assertNotNull(user);
assertAll("User",
() -> assertEquals("Alice", user.getName()),
() -> assertEquals(25, user.getAge()),
() -> assertEquals("alice@example.com", user.getEmail())
);
Summary
Use assertAll() like this:
assertAll("group name",
() -> assertEquals(expectedValue, actualValue),
() -> assertTrue(condition),
() -> assertNotNull(object)
);
It helps make your tests more informative by showing all assertion failures in one run instead of stopping at the first failure.
