Skip to content

Commit

Permalink
fix(JUnit Update): JUnit now has its own type for arguments
Browse files Browse the repository at this point in the history
Signed-off-by: l-1squared <30831153+l-1squared@users.noreply.github.com>
  • Loading branch information
l-1squared committed Oct 1, 2024
1 parent cd216ad commit 5290652
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions jgiven-junit5/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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";
Expand All @@ -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<NamedArgument> getNamedArgs( ExtensionContext context ) {
static List<NamedArgument> getNamedArgs(ExtensionContext context) {
List<NamedArgument> 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<Object> 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<Object> 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);
}
}

Expand Down

0 comments on commit 5290652

Please sign in to comment.