Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the first argument index.
Using the argument name instead of index.
✗ Incorrect
The first argument index is 0, so accessor.getString(0) correctly retrieves the first argument.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'Person' instead of the parameter.
Trying to access fields without checking the object.
✗ Incorrect
The parameter 'person' is the aggregated object, so assertNotNull(person) checks it is created.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using getString to get an integer value.
Using non-existent methods like getInt or getAge.
✗ Incorrect
ArgumentsAccessor provides getInteger(int index) to retrieve integer arguments.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name in the assertion.
Using the wrong age value for the assertion.
✗ Incorrect
The parameter name is 'person', and the first test case age is 28, so assertEquals(28, person.getAge()) is correct.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up argument indexes.
Using indexes outside the arguments range.
✗ Incorrect
The first name is at index 0, last name at 1, and age at 2 in the arguments.