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

Add step location in case of datatable conversion error #2908

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -18,7 +18,6 @@
import java.util.List;
import java.util.stream.Collectors;

import static io.cucumber.core.runner.StackManipulation.removeFrameworkFrames;
import static io.cucumber.core.runner.StackManipulation.removeFrameworkFramesAndAppendStepLocation;

class PickleStepDefinitionMatch extends Match implements StepDefinitionMatch {
Expand Down Expand Up @@ -51,13 +50,13 @@ public void runStep(TestCaseState state) throws Throwable {
} catch (CucumberExpressionException | CucumberDataTableException | CucumberDocStringException e) {
CucumberInvocationTargetException targetException;
if ((targetException = causedByCucumberInvocationTargetException(e)) != null) {
throw removeFrameworkFrames(targetException);
throw removeFrameworkFramesAndAppendStepLocation(targetException, getStepLocation());
}
throw couldNotConvertArguments(e);
} catch (CucumberBackendException e) {
throw couldNotInvokeArgumentConversion(e);
} catch (CucumberInvocationTargetException e) {
throw removeFrameworkFrames(e);
throw removeFrameworkFramesAndAppendStepLocation(e, getStepLocation());
}
try {
stepDefinition.execute(result.toArray(new Object[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Clock;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -236,7 +237,7 @@ void throws_could_not_convert_exception_for_transformer_and_capture_group_mismat
}

@Test
void rethrows_target_invocation_exceptions_from_parameter_type() {
void rethrows_target_invocation_exceptions_from_parameter_type() throws URISyntaxException {
RuntimeException userException = new RuntimeException();

stepTypeRegistry.defineParameterType(new ParameterType<>(
Expand All @@ -257,11 +258,15 @@ void rethrows_target_invocation_exceptions_from_parameter_type() {
StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
List<Argument> arguments = coreStepDefinition.matchedArguments(step);
StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition, null, step);
StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition,
new URI("test.feature"), step);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URI.create then the throws can be omitted. Here and all others.


Executable testMethod = () -> stepDefinitionMatch.runStep(null);
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, sameInstance(userException));
assertThat(
lastStackElement(actualThrown.getStackTrace()),
is(new StackTraceElement("✽", "I have some cukes in my belly", "test.feature", 3)));
}

@Test
Expand Down Expand Up @@ -290,7 +295,7 @@ void throws_could_not_convert_exception_for_singleton_table_dimension_mismatch()
}

@Test
void rethrows_target_invocation_exceptions_from_data_table() {
void rethrows_target_invocation_exceptions_from_data_table() throws URISyntaxException {
Feature feature = TestFeatureParser.parse("" +
"Feature: Test feature\n" +
" Scenario: Test scenario\n" +
Expand All @@ -312,11 +317,15 @@ void rethrows_target_invocation_exceptions_from_data_table() {
StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
List<Argument> arguments = coreStepDefinition.matchedArguments(step);
StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition, null, step);
StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition,
new URI("test.feature"), step);

Executable testMethod = () -> stepDefinitionMatch.runStep(null);
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, sameInstance(userException));
assertThat(
lastStackElement(actualThrown.getStackTrace()),
is(new StackTraceElement("✽", "I have some cukes in my belly", "test.feature", 3)));
}

@Test
Expand Down Expand Up @@ -347,7 +356,7 @@ void throws_could_not_convert_exception_for_docstring() {
}

@Test
void rethrows_target_invocation_exception_for_docstring() {
void rethrows_target_invocation_exception_for_docstring() throws URISyntaxException {
RuntimeException userException = new RuntimeException();

Feature feature = TestFeatureParser.parse("" +
Expand All @@ -367,11 +376,15 @@ void rethrows_target_invocation_exception_for_docstring() {
StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
List<Argument> arguments = coreStepDefinition.matchedArguments(step);
StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition, null, step);
StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition,
new URI("test.feature"), step);

Executable testMethod = () -> stepDefinitionMatch.runStep(null);
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, sameInstance(userException));
assertThat(
lastStackElement(actualThrown.getStackTrace()),
is(new StackTraceElement("✽", "I have some cukes in my belly", "test.feature", 3)));
}

@Test
Expand Down Expand Up @@ -465,6 +478,10 @@ void throws_could_not_invoke_step_when_execution_failed_with_null_arguments() {
"The converted arguments types were (null)")));
}

private StackTraceElement lastStackElement(StackTraceElement[] stackTrace) {
return stackTrace[stackTrace.length - 1];
}

private static final class ItemQuantity {

private final String s;
Expand Down