I have the following JUnit parameterized test case which uses an aggregator as well as a directly bonded argument. When I run it, I get the following error:
ParameterResolutionException: No ParameterResolver registered for parameter [java.lang.String arg1] in method [void PersonTest.testPersonGreeting(Person,java.lang.String)].
I think that JUnit can't know the index/column for the message argument because the PersonAggregator doesn't specify how many columns it uses. Is there a way to make this test work?
import org.junit.jupiter.api.extension.ParameterContext;
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.aggregator.ArgumentsAggregator;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PersonTest {
static 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, 'Hello Alice!'",
"Bob, 40, 'Hello Bob!'"
})
void testPersonGreeting(@AggregateWith(PersonAggregator.class) Person person, String message) {
assertEquals("Hello " + person.name() + "!", message);
}
}
record Person(String name, int age) {}