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

fix(Issue-1801): JGiven now can handle null parameters from JUnit5 #1804

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static List<NamedArgument> getNamedArgs(ExtensionContext context) {
&& 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());
List<Object> 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.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GivenStage, WhenStage, ThenStage> {

@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<Arguments> parametersWithNullInSubsequentTestCases() {
return Stream.of(
Arguments.of("Hello", "World"),
Arguments.of(null, "World"),
Arguments.of("Hello", null),
Arguments.of(null, null)
);
}

static Stream<Arguments> parametersWithNullInFirstTestCase() {
return Stream.of(
Arguments.of(null, "World"),
Arguments.of("Hello", "World")//,
);
}
}
Loading