Skip to content

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

Open
@marcphilipp

Description

@marcphilipp

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

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions