0
0
JUnittesting~10 mins

Argument aggregation in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how to use argument aggregation in JUnit 5 to combine multiple arguments into a single object for parameterized tests. It verifies that the aggregated object correctly holds the combined values.

Test Code - JUnit 5
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.AggregateWith;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.provider.CsvSource;

class User {
    String name;
    int age;

    User(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

class UserAggregator implements org.junit.jupiter.params.aggregator.ArgumentsAggregator {
    @Override
    public User aggregateArguments(ArgumentsAccessor accessor, java.lang.reflect.Parameter parameter) {
        return new User(accessor.getString(0), accessor.getInteger(1));
    }
}

public class ArgumentAggregationTest {

    @ParameterizedTest
    @CsvSource({"Alice,30", "Bob,25"})
    void testUserAggregation(@AggregateWith(UserAggregator.class) User user) {
        assertEquals("Alice", user.name, "First user's name should be Alice");
        assertEquals(30, user.age, "First user's age should be 30");
    }

    @ParameterizedTest
    @CsvSource({"Alice,30", "Bob,25"})
    void testUserAggregationDynamic(@AggregateWith(UserAggregator.class) User user) {
        if (user.name.equals("Alice")) {
            assertEquals(30, user.age);
        } else if (user.name.equals("Bob")) {
            assertEquals(25, user.age);
        } else {
            throw new AssertionError("Unexpected user name: " + user.name);
        }
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test runner starts and loads ArgumentAggregationTest classJUnit test environment initialized-PASS
2JUnit invokes testUserAggregation with first CSV input: "Alice,30"Test method called with aggregated User object (name=Alice, age=30)assertEquals("Alice", user.name) and assertEquals(30, user.age)PASS
3JUnit invokes testUserAggregation with second CSV input: "Bob,25"Test method called with aggregated User object (name=Bob, age=25)assertEquals("Alice", user.name) fails because user.name is "Bob"FAIL
4JUnit invokes testUserAggregationDynamic with first CSV input: "Alice,30"Test method called with aggregated User object (name=Alice, age=30)assertEquals(30, user.age) for user.name == "Alice"PASS
5JUnit invokes testUserAggregationDynamic with second CSV input: "Bob,25"Test method called with aggregated User object (name=Bob, age=25)assertEquals(25, user.age) for user.name == "Bob"PASS
Failure Scenario
Failing Condition: Using fixed assertions for specific input in testUserAggregation causes failure on second input
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @AggregateWith annotation do in this test?
AIt runs the test multiple times with different CSV inputs
BIt asserts that the User object is not null
CIt combines multiple CSV values into a single User object
DIt skips the test if the User object is invalid
Key Result
When using argument aggregation in parameterized tests, write assertions that adapt to each input value instead of fixed expected values to avoid false failures.