Skip to content

Add support for @FieldSource for @ParameterizedTest methods #638

Closed
@sbrannen

Description

@sbrannen

Overview

@FieldSource was introduced in JUnit Jupiter 5.11 for use with @ParameterizedTest methods.

Similar to @MethodSource which allows developers to supply a fully-qualified method name, @FieldSource allows developers to supply a fully-qualified field name.

Native Build Tools already registers reflection metadata for fully-qualified method names for @MethodSource here:

private static Class<?>[] handleMethodReference(String... methodNames) {
List<Class<?>> classList = new ArrayList<>();
for (String methodName : methodNames) {
String[] parts = methodName.split("#");
/*
* If the method used as an argument source resides in a different class than the test class, it must be specified
* by the fully qualified class name, followed by a # and the method name
*/
debug("Found method reference: %s", methodName);
if (parts.length == 2) {
String className = parts[0];
debug("Processing method reference from another class: %s", className);
try {
classList.add(Class.forName(className));
} catch (ClassNotFoundException e) {
debug("Failed to register method reference for reflection: %s Reason: %s", className, e);
}
} else {
debug("Skipping method reference as it originates in the same class as the test: %s", methodName);
}
}
return classList.toArray(new Class<?>[0]);
}

Similar support should be added for @FieldSource. For example, given the following parameterized test, reflection metadata should be registered for the example.FruitUtils#tropicalFruits field.

@ParameterizedTest
@FieldSource("example.FruitUtils#tropicalFruits")
void testWithExternalFieldSource(String tropicalFruit) {
    // test with tropicalFruit
}

Related Issues

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestjunit-supportRelated to JUnit Support project

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions