Challenge - 5 Problems
Argument Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JUnit test with argument aggregation
Consider the following JUnit 5 test using @CsvSource and argument aggregation. What will be the output when the test runs?
JUnit
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.aggregator.AggregateWith; import static org.junit.jupiter.api.Assertions.*; 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(org.junit.jupiter.params.aggregator.ArgumentsAccessor accessor, java.lang.reflect.Parameter parameter) { return new User(accessor.getString(0), accessor.getInteger(1)); } } public class UserTest { @ParameterizedTest @CsvSource({"Alice,30", "Bob,25"}) void testUser(@AggregateWith(UserAggregator.class) User user) { assertNotNull(user.name); assertTrue(user.age > 20); } }
Attempts:
2 left
💡 Hint
Think about how the aggregator converts CSV input into User objects and what the assertions check.
✗ Incorrect
The aggregator correctly creates User objects from the CSV input. Both users have non-null names and ages greater than 20, so all assertions pass.
❓ assertion
intermediate2:00remaining
Identify failing assertion in argument aggregation test
Given a parameterized test using argument aggregation, which assertion will fail if the aggregator incorrectly maps parameters?
JUnit
import static org.junit.jupiter.api.Assertions.*; class Product { String id; double price; Product(String id, double price) { this.id = id; this.price = price; } } // Aggregator maps first param to price, second to id (incorrect) class ProductAggregator implements org.junit.jupiter.params.aggregator.ArgumentsAggregator { @Override public Product aggregateArguments(org.junit.jupiter.params.aggregator.ArgumentsAccessor accessor, java.lang.reflect.Parameter parameter) { return new Product(accessor.getString(1), accessor.getDouble(0)); } } @org.junit.jupiter.params.ParameterizedTest @org.junit.jupiter.params.provider.CsvSource({"19.99, P123", "5.50, P456"}) void testProduct(@org.junit.jupiter.params.aggregator.AggregateWith(ProductAggregator.class) Product product) { assertNotNull(product.id); assertTrue(product.price > 0); }
Attempts:
2 left
💡 Hint
Check how the aggregator swaps parameters and what values are passed.
✗ Incorrect
Although the aggregator swaps parameters, the CSV source provides price first and id second, so product.id gets the string id and product.price gets the double price correctly. Both assertions pass.
🔧 Debug
advanced2:00remaining
Debug failing argument aggregation test due to missing parameter
A parameterized test using argument aggregation fails with an IndexOutOfBoundsException. What is the most likely cause?
JUnit
import static org.junit.jupiter.api.Assertions.*; @org.junit.jupiter.params.ParameterizedTest @org.junit.jupiter.params.provider.CsvSource({"John", "Jane, 28"}) void testPerson(@org.junit.jupiter.params.aggregator.AggregateWith(PersonAggregator.class) Person person) { assertNotNull(person.name); assertTrue(person.age > 0); } class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } } class PersonAggregator implements org.junit.jupiter.params.aggregator.ArgumentsAggregator { @Override public Person aggregateArguments(org.junit.jupiter.params.aggregator.ArgumentsAccessor accessor, java.lang.reflect.Parameter parameter) { return new Person(accessor.getString(0), accessor.getInteger(1)); } }
Attempts:
2 left
💡 Hint
Look at the CSV input lines and how many parameters each has.
✗ Incorrect
The first CSV line has only one parameter (name), but the aggregator expects two parameters (name and age). Trying to access the second parameter causes IndexOutOfBoundsException.
❓ framework
advanced2:00remaining
Best practice for argument aggregation in JUnit 5
Which practice is recommended when implementing a custom ArgumentsAggregator in JUnit 5?
Attempts:
2 left
💡 Hint
Think about how JUnit uses reflection to instantiate aggregators.
✗ Incorrect
JUnit requires aggregators to be static nested or top-level classes so it can instantiate them via reflection without needing an instance of the outer class. Using instance variables or lifecycle methods is not recommended.
🧠 Conceptual
expert3:00remaining
Understanding argument aggregation behavior with nested objects
In JUnit 5, when using argument aggregation to create a complex object with nested objects, which statement is true?
Attempts:
2 left
💡 Hint
Consider how argument aggregation works with complex parameter types.
✗ Incorrect
JUnit's argument aggregation works at the top level and does not recursively aggregate nested objects. You must manually create nested objects inside your aggregator implementation.