Bird
Raised Fist0
JUnittesting~20 mins

Argument aggregation in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Argument Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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);
    }
}
ATest passes for both Alice and Bob
BTest fails for Alice because age is not > 20
CTest fails for Bob because name is null
DTest throws a runtime exception due to aggregator error
Attempts:
2 left
💡 Hint
Think about how the aggregator converts CSV input into User objects and what the assertions check.
❓ assertion
intermediate
2: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);
}
AassertNotNull(product.id) fails
BassertTrue(product.price > 0) fails
CTest throws NullPointerException
DBoth assertions pass
Attempts:
2 left
💡 Hint
Check how the aggregator swaps parameters and what values are passed.
🔧 Debug
advanced
2: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));
    }
}
AThe first CSV line has missing age parameter causing IndexOutOfBoundsException
BPersonAggregator class is not public causing reflection failure
CThe test method is missing @Test annotation
DPerson constructor parameters are in wrong order
Attempts:
2 left
💡 Hint
Look at the CSV input lines and how many parameters each has.
❓ framework
advanced
2:00remaining
Best practice for argument aggregation in JUnit 5
Which practice is recommended when implementing a custom ArgumentsAggregator in JUnit 5?
AThrow RuntimeException inside aggregateArguments to fail tests on invalid input
BUse instance variables in the aggregator to store state between test runs
CMake the aggregator class a static nested class or top-level class to avoid reflection issues
DUse @BeforeEach to initialize aggregator fields before each aggregation
Attempts:
2 left
💡 Hint
Think about how JUnit uses reflection to instantiate aggregators.
🧠 Conceptual
expert
3: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?
AJUnit automatically aggregates nested objects if their aggregators are registered globally
BYou must manually aggregate nested objects inside the main aggregator; JUnit does not recursively aggregate nested parameters
CNested objects cannot be used with argument aggregation and require separate parameterized tests
DJUnit uses reflection to automatically instantiate nested objects without custom aggregators
Attempts:
2 left
💡 Hint
Consider how argument aggregation works with complex parameter types.

Practice

(1/5)
1. What is the main purpose of using @ArgumentsAggregator in JUnit parameterized tests?
easy
A. To combine multiple method arguments into a single object for easier testing
B. To run tests in parallel threads
C. To generate random test data automatically
D. To skip certain test cases during execution

Solution

  1. Step 1: Understand the role of @ArgumentsAggregator

    This annotation is used to create a custom aggregator that combines multiple input arguments into one object.
  2. Step 2: Recognize the benefit in parameterized tests

    Aggregating arguments simplifies test method signatures and improves readability by passing one object instead of many parameters.
  3. Final Answer:

    To combine multiple method arguments into a single object for easier testing -> Option A
  4. Quick Check:

    @ArgumentsAggregator = combine inputs [OK]
Hint: Remember: Aggregator groups inputs into one object [OK]
Common Mistakes:
  • Confusing @ArgumentsAggregator with parallel test execution
  • Thinking it generates test data automatically
  • Assuming it skips tests
2. Which of the following is the correct way to apply a custom aggregator in a JUnit 5 parameterized test method parameter?
easy
A. @AggregateWith(CustomAggregator.class) MyObject obj
B. @ArgumentsSource(CustomAggregator.class) MyObject obj
C. @AggregateArguments(CustomAggregator.class) MyObject obj
D. @ArgumentAggregator(CustomAggregator.class) MyObject obj

Solution

  1. Step 1: Recall the correct annotation for applying aggregators

    The annotation to apply a custom aggregator on a parameter is @AggregateWith.
  2. Step 2: Match the syntax

    The correct syntax is @AggregateWith(CustomAggregator.class) followed by the parameter type and name.
  3. Final Answer:

    @AggregateWith(CustomAggregator.class) MyObject obj -> Option A
  4. Quick Check:

    @AggregateWith = apply aggregator [OK]
Hint: Use @AggregateWith to link aggregator class to parameter [OK]
Common Mistakes:
  • Using @ArgumentsSource instead of @AggregateWith
  • Misspelling the annotation name
  • Confusing @AggregateWith with @ArgumentAggregator
3. Given the following aggregator implementation and test method, what will be the output of the assertion?
public class PersonAggregator implements ArgumentsAggregator {
  @Override
  public Person aggregateArguments(ArgumentsAccessor accessor, ParameterContext context) {
    return new Person(accessor.getString(0), accessor.getInteger(1));
  }
}

@ParameterizedTest
@CsvSource({"Alice,30", "Bob,25"})
void testPerson(@AggregateWith(PersonAggregator.class) Person person) {
  assertTrue(person.getAge() > 20);
}
medium
A. Test fails for Alice because age is 30
B. Test fails for Bob because age is 25
C. Test passes for both Alice and Bob
D. Test fails for both because age is not checked correctly

Solution

  1. Step 1: Understand the aggregator behavior

    The aggregator creates a Person object with name and age from CSV input.
  2. Step 2: Evaluate the assertion for each input

    Both Alice (30) and Bob (25) have ages greater than 20, so assertTrue(person.getAge() > 20) passes for both.
  3. Final Answer:

    Test passes for both Alice and Bob -> Option C
  4. Quick Check:

    Age > 20 for both inputs = pass [OK]
Hint: Check each input value against assertion condition [OK]
Common Mistakes:
  • Assuming 25 is less than or equal to 20
  • Confusing assertion logic
  • Ignoring aggregator mapping
4. Identify the error in this aggregator implementation:
public class ProductAggregator implements ArgumentsAggregator {
  @Override
  public Product aggregateArguments(ArgumentsAccessor accessor, ParameterContext context) {
    String name = accessor.getString(0);
    int price = accessor.getInteger(1);
    return new Product(name, price);
  }
}

@ParameterizedTest
@CsvSource({"Laptop,1000", "Phone,abc"})
void testProduct(@AggregateWith(ProductAggregator.class) Product product) {
  assertNotNull(product);
}
medium
A. The aggregator method signature is incorrect
B. The Product constructor parameters are in wrong order
C. The @AggregateWith annotation is missing
D. The second CSV input 'Phone,abc' causes a runtime error due to invalid integer

Solution

  1. Step 1: Analyze CSV input and aggregator code

    The second CSV line has 'abc' where an integer price is expected.
  2. Step 2: Understand the impact on aggregation

    Calling accessor.getInteger(1) on 'abc' will throw a runtime exception (NumberFormatException).
  3. Final Answer:

    The second CSV input 'Phone,abc' causes a runtime error due to invalid integer -> Option D
  4. Quick Check:

    Invalid integer input = runtime error [OK]
Hint: Check CSV data types match aggregator expectations [OK]
Common Mistakes:
  • Ignoring invalid CSV data causing exceptions
  • Assuming aggregator method signature is wrong
  • Thinking annotation is missing
5. You want to test a method that takes a Person object with fields name and age. You have CSV data with columns name, age, and city. How can you write a custom aggregator to ignore the city column and only aggregate name and age into Person?
hard
A. Use @AggregateWith but do not implement ArgumentsAggregator interface
B. Use ArgumentsAccessor to get only the first two columns and create Person ignoring city
C. Use @CsvSource with only two columns to avoid city
D. Use ArgumentsAccessor to get all three columns and pass city to Person constructor

Solution

  1. Step 1: Understand the CSV data and Person constructor

    CSV has three columns but Person only needs name and age.
  2. Step 2: Implement aggregator to pick only needed columns

    Use ArgumentsAccessor.getString(0) and getInteger(1) to get name and age, ignoring the third city column.
  3. Final Answer:

    Use ArgumentsAccessor to get only the first two columns and create Person ignoring city -> Option B
  4. Quick Check:

    Pick needed columns only in aggregator [OK]
Hint: Pick only needed columns with ArgumentsAccessor [OK]
Common Mistakes:
  • Trying to pass extra columns to Person constructor
  • Changing CSV source instead of aggregator logic
  • Not implementing ArgumentsAggregator interface
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard