-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using reflection to get the arguments of a JUnit 5 ParameterizedTest. F…
…ixes #372
- Loading branch information
Jan Schäfer
committed
Oct 28, 2018
1 parent
727f9c5
commit f82dcd4
Showing
6 changed files
with
104 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
jgiven-junit5/src/main/java/com/tngtech/jgiven/junit5/ArgumentReflectionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.tngtech.jgiven.junit5; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.tngtech.jgiven.impl.util.ParameterNameUtil; | ||
import com.tngtech.jgiven.impl.util.ReflectionUtil; | ||
import com.tngtech.jgiven.report.model.NamedArgument; | ||
|
||
class ArgumentReflectionUtil { | ||
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"; | ||
static final String PARAMETERIZED_TEST_INVOCATION_CONTEXT = "org.junit.jupiter.params.ParameterizedTestInvocationContext"; | ||
|
||
static final String ERROR = "Not able to access field containing test method arguments. " + | ||
"Probably the internal representation has changed. Consider writing a bug report."; | ||
|
||
/** | ||
* This is a very ugly workaround to get the method arguments from the JUnit 5 context via reflection. | ||
*/ | ||
static List<NamedArgument> getNamedArgs( ExtensionContext context ) { | ||
List<NamedArgument> namedArgs = new ArrayList<>(); | ||
|
||
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 ); | ||
} | ||
} | ||
} | ||
} catch( Exception e ) { | ||
log.warn( ERROR, e ); | ||
} | ||
} | ||
|
||
return namedArgs; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/ParameterizedTestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.tngtech.jgiven.junit5.test; | ||
|
||
import static org.assertj.core.api.Java6Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import com.tngtech.jgiven.annotation.CaseAs; | ||
import com.tngtech.jgiven.junit5.JGivenExtension; | ||
import com.tngtech.jgiven.junit5.ScenarioTest; | ||
|
||
@ExtendWith( JGivenExtension.class ) | ||
public class ParameterizedTestTest extends ScenarioTest<GivenStage, WhenStage, ThenStage> { | ||
|
||
@ParameterizedTest( name = "{index} [{arguments}] param name" ) | ||
@ValueSource( strings = { "Hello", "World" } ) | ||
@CaseAs( "Case $1" ) | ||
public void parameterized_scenario( String param ) { | ||
given().some_state(); | ||
when().some_action_with_a_parameter( param ); | ||
then().some_outcome(); | ||
|
||
assertThat( getScenario().getScenarioCaseModel().getDescription() ).isIn( "Case Hello", "Case World" ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters