0
0
JUnittesting~10 mins

assertAll for grouped assertions in JUnit - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit 5
JUnit
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("@"))
        );
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsJUnit test runner initialized-PASS
2Creates Person object with name 'Alice', age 30, email 'alice@example.com'Person instance in memory with given properties-PASS
3Runs assertAll grouping three assertionsAssertions prepared for name, age, and emailCheck name equals 'Alice', age equals 30, email contains '@'PASS
4Each assertion runs inside assertAllAssertions executed sequentiallyassertEquals("Alice", person.getName()) passesPASS
5Second assertion runsChecking ageassertEquals(30, person.getAge()) passesPASS
6Third assertion runsChecking email contains '@'assertTrue(person.getEmail().contains("@")) passesPASS
7assertAll completes, test endsAll grouped assertions passedGrouped assertions all passedPASS
Failure Scenario
Failing Condition: One or more assertions inside assertAll fail, e.g., email does not contain '@'
Execution Trace Quiz - 3 Questions
Test your understanding
What does assertAll do in this test?
AStops test at first failed assertion
BRuns all assertions and reports all failures together
CSkips assertions if the first one passes
DRuns only one assertion randomly
Key Result
Using assertAll groups multiple assertions so you see all failures at once, making debugging easier and tests more informative.