From 5487ec64b004b0e9c4b9d394fe95ed4608631f9a Mon Sep 17 00:00:00 2001 From: l-1squared <30831153+l-1squared@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:32:20 +0100 Subject: [PATCH] fix(Issue-1801): JGiven now can handle null parameters from JUnit5 According to the test description this only shows up when the null value is in the second argument. I didn't quite understand why this is the case. But since the distincition between the first and subsequent cases is important, I've added a respective case. Signed-off-by: l-1squared <30831153+l-1squared@users.noreply.github.com> --- .../jgiven/junit5/ArgumentReflectionUtil.java | 2 +- .../junit5/test/ParameterizedTestTest.java | 57 ++++++++++++++++--- 2 files changed, 50 insertions(+), 9 deletions(-) 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 aec667953f..493deaa885 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 @@ -42,7 +42,7 @@ static List getNamedArgs(ExtensionContext context) { && 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()); + List args = Arrays.asList(((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."); diff --git a/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/ParameterizedTestTest.java b/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/ParameterizedTestTest.java index bf24873bc1..8535422f9f 100644 --- a/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/ParameterizedTestTest.java +++ b/jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/ParameterizedTestTest.java @@ -4,24 +4,65 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; +import org.junit.jupiter.params.provider.*; import com.tngtech.jgiven.annotation.CaseAs; import com.tngtech.jgiven.junit5.JGivenExtension; import com.tngtech.jgiven.junit5.ScenarioTest; -@ExtendWith( JGivenExtension.class ) +import java.util.stream.Stream; + +@ExtendWith(JGivenExtension.class) public class ParameterizedTestTest extends ScenarioTest { - @ParameterizedTest( name = "{index} [{arguments}] param name" ) - @ValueSource( strings = { "Hello", "World" } ) - @CaseAs( "Case $1" ) - public void parameterized_scenario( String param ) { + @ParameterizedTest(name = "{index} [{arguments}] param name") + @ValueSource(strings = {"Hello", "World"}) + @NullSource + @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", "Case null"); + } + + @ParameterizedTest + @MethodSource("parametersWithNullInSubsequentTestCases") + void parameterized_scenario_with_null_arguments_in_subsequent_test_cases(String param1, String param2) { given().some_state(); - when().some_action_with_a_parameter( param ); + when().some_action_with_a_parameter(param1); + when().some_action_with_a_parameter(param2); then().some_outcome(); - assertThat( getScenario().getScenarioCaseModel().getDescription() ).isIn( "Case Hello", "Case World" ); + assertThat(getScenario().getScenarioCaseModel().getExplicitArguments()).containsExactly(String.valueOf(param1), String.valueOf(param2)); } + + @ParameterizedTest + @MethodSource("parametersWithNullInFirstTestCase") + void parameterized_scenario_with_null_arguments_in_first_iteration(String param1, String param2) { + given().some_state(); + when().some_action_with_a_parameter(param1); + when().some_action_with_a_parameter(param2); + then().some_outcome(); + + assertThat(getScenario().getScenarioCaseModel().getExplicitArguments()).containsExactly(String.valueOf(param1), String.valueOf(param2)); + } + + static Stream parametersWithNullInSubsequentTestCases() { + return Stream.of( + Arguments.of("Hello", "World"), + Arguments.of(null, "World"), + Arguments.of("Hello", null), + Arguments.of(null, null) + ); + } + + static Stream parametersWithNullInFirstTestCase() { + return Stream.of( + Arguments.of(null, "World"), + Arguments.of("Hello", "World")//, + ); + } }