Test Overview
This test checks multiple properties of a Person object using assertAll to group assertions. It verifies the name, age, and email in one test run.
This test checks multiple properties of a Person object using assertAll to group assertions. It verifies the name, age, and email in one test run.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class Person { private String name; private int age; private String email; Person(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 PersonTest { @Test void testPersonProperties() { Person person = new Person("Alice", 30, "alice@example.com"); assertAll("person properties", () -> assertEquals("Alice", person.getName()), () -> assertEquals(30, person.getAge()), () -> assertTrue(person.getEmail().contains("@")) ); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | — | PASS |
| 2 | Creates Person object with name 'Alice', age 30, email 'alice@example.com' | Person instance in memory with given properties | — | PASS |
| 3 | Runs assertAll grouping three assertions | Assertions prepared for name, age, and email | Check name equals 'Alice', age equals 30, email contains '@' | PASS |
| 4 | Each assertion runs inside assertAll | Assertions executed sequentially | assertEquals("Alice", person.getName()) passes | PASS |
| 5 | Second assertion runs | Checking age | assertEquals(30, person.getAge()) passes | PASS |
| 6 | Third assertion runs | Checking email contains '@' | assertTrue(person.getEmail().contains("@")) passes | PASS |
| 7 | assertAll completes, test ends | All grouped assertions passed | Grouped assertions all passed | PASS |
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