In JUnit, I am using ParameterResolver
to dynamically create and inject method argument to a Test
, this works for non generic methods like the one below which is expecting Person peron
. How can I instead change it to List<Person> personList
and have the supportsParameter
check if List
is of Person
@ExtendWith({AnnotationProcessor.class})
public void testInject(Person person) {
System.out.println(person);
}
AnnotationProcessor.java
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
return Person.class.equals(parameter.getType());
}
The closest I was able to use is :
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
Parameter parameter = parameterContext.getParameter();
if (parameter.getParameterizedType()!= null && List.class.equals(parameter.getType())) {
if (parameter.getParameterizedType() instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType)parameter.getParameterizedType();
Class<?> clazz = (Class<?>)pt.getActualTypeArguments()[0];
if (clazz == Person.class) return true;
}
}
return false;
}