diff --git a/jgiven-junit5/build.gradle b/jgiven-junit5/build.gradle index f41da7aaff..e224165baa 100644 --- a/jgiven-junit5/build.gradle +++ b/jgiven-junit5/build.gradle @@ -17,6 +17,7 @@ dependencies { api project(':jgiven-core') implementation(platform(libs.junit.bom)) + compileOnly 'org.junit.jupiter:junit-jupiter-params' compileOnly 'org.junit.jupiter:junit-jupiter-api' testImplementation project(':jgiven-html5-report') diff --git a/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/ArgumentReflectionUtil.java b/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/ArgumentReflectionUtil.java index c373627825..aec667953f 100644 --- a/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/ArgumentReflectionUtil.java +++ b/jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/ArgumentReflectionUtil.java @@ -6,6 +6,7 @@ import java.util.List; import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +15,7 @@ import com.tngtech.jgiven.report.model.NamedArgument; class ArgumentReflectionUtil { - private static final Logger log = LoggerFactory.getLogger( ArgumentReflectionUtil.class ); + private static final Logger log = LoggerFactory.getLogger(ArgumentReflectionUtil.class); static final String METHOD_EXTENSION_CONTEXT = "org.junit.jupiter.engine.descriptor.MethodExtensionContext"; static final String TEST_TEMPLATE_INVOCATION_TEST_DESCRIPTOR = "org.junit.jupiter.engine.descriptor.TestTemplateInvocationTestDescriptor"; @@ -26,27 +27,31 @@ class ArgumentReflectionUtil { /** * This is a very ugly workaround to get the method arguments from the JUnit 5 context via reflection. */ - static List getNamedArgs( ExtensionContext context ) { + static List getNamedArgs(ExtensionContext context) { List namedArgs = new ArrayList<>(); - if( context.getTestMethod().get().getParameterCount() > 0 ) { + if (context.getTestMethod().get().getParameterCount() > 0) { try { - if( context.getClass().getCanonicalName().equals( METHOD_EXTENSION_CONTEXT ) ) { - Field field = context.getClass().getSuperclass().getDeclaredField( "testDescriptor" ); - Object testDescriptor = ReflectionUtil.getFieldValueOrNull( field, context, ERROR ); - if( testDescriptor != null - && testDescriptor.getClass().getCanonicalName().equals( TEST_TEMPLATE_INVOCATION_TEST_DESCRIPTOR ) ) { - Object invocationContext = ReflectionUtil.getFieldValueOrNull( "invocationContext", testDescriptor, ERROR ); - if( invocationContext != null - && invocationContext.getClass().getCanonicalName().equals( PARAMETERIZED_TEST_INVOCATION_CONTEXT ) ) { - Object arguments = ReflectionUtil.getFieldValueOrNull( "arguments", invocationContext, ERROR ); - List args = Arrays.asList( (Object[]) arguments ); - namedArgs = ParameterNameUtil.mapArgumentsWithParameterNames( context.getTestMethod().get(), args ); + if (context.getClass().getCanonicalName().equals(METHOD_EXTENSION_CONTEXT)) { + Field field = context.getClass().getSuperclass().getDeclaredField("testDescriptor"); + Object testDescriptor = ReflectionUtil.getFieldValueOrNull(field, context, ERROR); + if (testDescriptor != null + && testDescriptor.getClass().getCanonicalName().equals(TEST_TEMPLATE_INVOCATION_TEST_DESCRIPTOR)) { + Object invocationContext = ReflectionUtil.getFieldValueOrNull("invocationContext", testDescriptor, ERROR); + if (invocationContext != null + && invocationContext.getClass().getCanonicalName().equals(PARAMETERIZED_TEST_INVOCATION_CONTEXT)) { + Object arguments = ReflectionUtil.getFieldValueOrNull("arguments", invocationContext, ERROR); + if (arguments instanceof Arguments) { + List args = List.of(((Arguments) arguments).get()); + namedArgs = ParameterNameUtil.mapArgumentsWithParameterNames(context.getTestMethod().get(), args); + } else { + log.warn(ERROR + " The type of arguments in the invocation context has changed. Please write a bug report."); + } } } } - } catch( Exception e ) { - log.warn( ERROR, e ); + } catch (Exception e) { + log.warn(ERROR, e); } }