Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for @FieldSource for @ParameterizedTest methods #638

Open
sbrannen opened this issue Nov 5, 2024 · 0 comments
Open

Add support for @FieldSource for @ParameterizedTest methods #638

sbrannen opened this issue Nov 5, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request junit-support Related to JUnit Support project

Comments

@sbrannen
Copy link
Collaborator

sbrannen commented Nov 5, 2024

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

@sbrannen sbrannen added enhancement New feature or request junit-support Related to JUnit Support project labels Nov 5, 2024
@dnestoro dnestoro self-assigned this Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request junit-support Related to JUnit Support project
Projects
None yet
Development

No branches or pull requests

2 participants