Verify multiple properties of a User object using grouped assertions
Preconditions (2)
✅ Expected Result: All assertions inside assertAll run and report failures together if any property does not match expected values
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class User { private final String name; private final int age; private final String email; User(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } String getName() { return name; } int getAge() { return age; } String getEmail() { return email; } } public class UserTest { @Test void testUserProperties() { User user = new User("Alice", 30, "alice@example.com"); assertAll("User properties", () -> assertEquals("Alice", user.getName(), "Name should be Alice"), () -> assertEquals(30, user.getAge(), "Age should be 30"), () -> assertEquals("alice@example.com", user.getEmail(), "Email should be alice@example.com") ); } }
This test defines a simple User class with three properties: name, age, and email.
In the testUserProperties() method, we create a User object with expected values.
We use assertAll to group three assertions. Each assertion checks one property using assertEquals.
Using lambda expressions inside assertAll ensures all assertions run, and if any fail, JUnit reports all failures together.
This approach helps quickly see all mismatches without stopping at the first failure.
Now add a second User object with different properties and verify both users' properties using assertAll grouping.
assertAll in JUnit tests?assertAll groups multiple assertions and executes them all, regardless of failures.assertAll does not stop early, fix errors, or run assertions in parallel.assertAll in JUnit 5?assertAll takes lambda expressions as arguments, each representing an assertion.assertAll( () -> assertEquals(2, 1 + 1), () -> assertTrue(3 > 5), () -> assertFalse(false) );
assertAll:assertAll( () -> assertEquals(10, calculate()), assertTrue(isReady()) );
assertAll must be a lambda expression or executable.assertTrue(isReady()) is a method call, not a lambda. It should be wrapped as () -> assertTrue(isReady()).Person object's fields name, age, and email together using assertAll. Which code snippet correctly groups these assertions and reports all failures at once?Person p = new Person("Alice", 30, "alice@example.com");
// Choose the correct assertAll usage:assertAll expects multiple lambda expressions as separate arguments, each containing one assertion.Pass the test and this pattern moves unknown