0
0
JUnittesting~10 mins

Argument aggregation in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an argument aggregator class that implements ArgumentsAggregator.

JUnit
public class PersonAggregator implements ArgumentsAggregator {
    @Override
    public Person aggregateArguments(ArgumentsAccessor accessor, ParameterContext context) {
        String name = accessor.getString([1]);
        int age = accessor.getInteger(1);
        return new Person(name, age);
    }
}
Drag options to blanks, or click blank then click option'
Aname
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the first argument index.
Using the argument name instead of index.
2fill in blank
medium

Complete the code to use the custom aggregator in a parameterized test.

JUnit
@ParameterizedTest
@CsvSource({"John,25", "Jane,30"})
void testPerson(@AggregateWith(PersonAggregator.class) Person person) {
    assertNotNull([1]);
}
Drag options to blanks, or click blank then click option'
APerson
Bperson.name
Cperson
Dthis.person
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'Person' instead of the parameter.
Trying to access fields without checking the object.
3fill in blank
hard

Fix the error in the aggregator method to correctly retrieve the age argument as an integer.

JUnit
public Person aggregateArguments(ArgumentsAccessor accessor, ParameterContext context) {
    String name = accessor.getString(0);
    int age = accessor.[1](1);
    return new Person(name, age);
}
Drag options to blanks, or click blank then click option'
AgetString
BgetInteger
CgetInt
DgetAge
Attempts:
3 left
💡 Hint
Common Mistakes
Using getString to get an integer value.
Using non-existent methods like getInt or getAge.
4fill in blank
hard

Fill both blanks to create a parameterized test method that uses the aggregator and asserts the person's age.

JUnit
@ParameterizedTest
@CsvSource({"Alice,28"})
void testPersonAge(@AggregateWith(PersonAggregator.class) Person [1]) {
    assertEquals([2], [1].getAge());
}
Drag options to blanks, or click blank then click option'
Aperson
Bage
C28
D35
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name in the assertion.
Using the wrong age value for the assertion.
5fill in blank
hard

Fill all three blanks to implement an aggregator that combines first and last names into fullName and returns a Person.

JUnit
public class FullNameAggregator implements ArgumentsAggregator {
    @Override
    public Person aggregateArguments(ArgumentsAccessor accessor, ParameterContext context) {
        String firstName = accessor.getString([1]);
        String lastName = accessor.getString([2]);
        String fullName = firstName + " " + lastName;
        int age = accessor.getInteger([3]);
        return new Person(fullName, age);
    }
}
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up argument indexes.
Using indexes outside the arguments range.