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

@ParameterizedTest with @EnumSource are not supported. #50

Closed
michael-simons opened this issue Jun 15, 2021 · 1 comment · Fixed by #59
Closed

@ParameterizedTest with @EnumSource are not supported. #50

michael-simons opened this issue Jun 15, 2021 · 1 comment · Fixed by #59
Labels
bug Something isn't working junit-support Related to JUnit Support project
Milestone

Comments

@michael-simons
Copy link

A parameterized tests with an @EnumSource like this

package org.graalvm.buildtools.example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

class App2Test {

	enum Things {
		A, B, C
	}

	@ParameterizedTest
	@EnumSource(Things.class)
	void functionNameSanityCheck(Things thing) {

		Assertions.assertNotNull(thing);
	}

}

fails with

Failures (1):
  JUnit Jupiter:App2Test:functionNameSanityCheck(Things)
    MethodSource [className = 'org.graalvm.buildtools.example.App2Test', methodName = 'functionNameSanityCheck', methodParameterTypes = 'org.graalvm.buildtools.example.App2Test$Things']
    => org.junit.platform.commons.JUnitException: Failed to find a no-argument constructor for ArgumentsProvider [org.junit.jupiter.params.provider.EnumArgumentsProvider]. Please ensure that a no-argument constructor exists and that the class is either a top-level class or a static nested class
       org.junit.jupiter.params.ParameterizedTestExtension.instantiateArgumentsProvider(ParameterizedTestExtension.java:110)
       java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
       java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
       java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
       java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
       [...]
       Suppressed: org.junit.platform.commons.PreconditionViolationException: Configuration error: You must configure at least one set of arguments for this @ParameterizedTest
         org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:281)
         org.junit.jupiter.params.ParameterizedTestExtension.lambda$provideTestTemplateInvocationContexts$6(ParameterizedTestExtension.java:94)
         java.util.stream.AbstractPipeline.close(AbstractPipeline.java:323)
         java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:271)
         [...]
     Caused by: java.lang.NoSuchMethodException: org.junit.jupiter.params.provider.EnumArgumentsProvider.<init>()
       java.lang.Class.getConstructor0(DynamicHub.java:3349)
       java.lang.Class.getDeclaredConstructor(DynamicHub.java:2553)
       org.junit.platform.commons.util.ReflectionUtils.newInstance(ReflectionUtils.java:488)
       org.junit.jupiter.params.ParameterizedTestExtension.instantiateArgumentsProvider(ParameterizedTestExtension.java:102)
       [...]

Applies to 0.9.0 and 0.9.1-SNAPSHOT, tested with GraalVM 21.1.0 JDK 11 in the Maven examples project.

@sbrannen
Copy link
Collaborator

sbrannen commented Jun 16, 2021

As a workaround, running native tests with the agent enabled should hopefully pick up this use of reflection. The agent is supported with Gradle but currently not an option with Maven (see #13).

Another workaround is to manually add the necessary reflection config for the JUnit Jupiter types that are loaded via reflection.

In general, all org.junit.jupiter.params.provider.ArgumentsProvider implementations supported by junit-jupiter-params should be added to the Feature, like with the CsvArgumentsProvider here:

RuntimeReflection.registerForReflectiveInstantiation(Class.forName("org.junit.jupiter.params.provider.CsvArgumentsProvider"));

However, that would not support a user's custom ArgumentsProvider implementation or those from third parties.

This actually touches on a broader topic that needs to be addressed regarding types loaded via reflection via @ExtendWith, @ArgumentsSource, @EnabledIf, @DisabledIf, @MethodSource, etc. So I'll create another ticket to start the discussion (see #54).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working junit-support Related to JUnit Support project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants