Skip to content

Ensure GraalVM native build tools support @Nested tests in @ParameterizedClasses #4440

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

Open
1 task
marcphilipp opened this issue Apr 1, 2025 · 0 comments
Open
1 task

Comments

@marcphilipp
Copy link
Member

marcphilipp commented Apr 1, 2025

Currently, @Nested test classes in enclosing @ParameterizedClasses are not registered for reflection which causes errors like the following when running tests in the native image:

org.junit.platform.commons.PreconditionViolationException: Class [com.example.project.CalculatorParameterizedClassTests$Inner] must declare a single constructor but there were none
       org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:313)
       org.junit.platform.commons.util.ReflectionUtils.getDeclaredConstructor(ReflectionUtils.java:1338)
       org.junit.jupiter.engine.descriptor.ExtensionUtils.registerExtensionsFromConstructorParameters(ExtensionUtils.java:174)
       org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.prepare(ClassBasedTestDescriptor.java:187)
       org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.prepare(ClassBasedTestDescriptor.java:87)
       org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$2(NodeTestTask.java:129)
       org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
       org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:129)
       org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:96)
       [email protected]/java.util.ArrayList.forEach(ArrayList.java:1596)
       [...]

In order to make reflection work, the JUnitPlatformFeature of native-build-tools traverses the TestPlan and registers all classes from TestIdentifier instances with a ClassSource for reflection: https://github.com/graalvm/native-build-tools/blob/c4c34356af1427bf4957305b40111f253bc4762a/common/junit-platform-native/src/main/java/org/graalvm/junit/platform/JUnitPlatformFeature.java#L128-L148

While that works for regular @Nested classes, children of @ParameterizedClass are not part of the TestPlan since they are created dynamically at runtime.

I see two options for fixing this:

  1. Change native-build-tools to use a PostDiscoveryFilter which would see the @Nested classes in @ParameterizedClass ones before they're pruned instead of inspecting the TestPlan
  2. Keep inspecting the TestPlan but register all inner classes (or all @Nested-annotated inner classes) for reflection in addition to the top-level test classes

Deliverables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment