use of io.cucumber.core.backend.CucumberInvocationTargetException in project cucumber-jvm by cucumber.
the class JavaStepDefinitionTest method can_provide_location_of_step.
@Test
void can_provide_location_of_step() throws Throwable {
Method method = JavaStepDefinitionTest.class.getMethod("method_throws");
JavaStepDefinition definition = new JavaStepDefinition(method, "three (.*) mice", lookup);
CucumberInvocationTargetException exception = assertThrows(CucumberInvocationTargetException.class, () -> definition.execute(new Object[0]));
Optional<StackTraceElement> match = stream(exception.getInvocationTargetExceptionCause().getStackTrace()).filter(definition::isDefinedAt).findFirst();
StackTraceElement stackTraceElement = match.get();
assertAll(() -> assertThat(stackTraceElement.getMethodName(), is("method_throws")), () -> assertThat(stackTraceElement.getClassName(), is(JavaStepDefinitionTest.class.getName())));
}
use of io.cucumber.core.backend.CucumberInvocationTargetException in project cucumber-jvm by cucumber.
the class StepDefinitionMatchTest method rethrows_target_invocation_exception_for_docstring.
@Test
void rethrows_target_invocation_exception_for_docstring() {
RuntimeException userException = new RuntimeException();
Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + " Scenario: Test scenario\n" + " Given I have some cukes in my belly\n" + " \"\"\"doc\n" + " converting this should throw an exception\n" + " \"\"\"\n");
stepTypeRegistry.defineDocStringType(new DocStringType(ItemQuantity.class, "doc", content -> {
throw new CucumberInvocationTargetException(stubbedLocation, new InvocationTargetException(userException));
}));
Step step = feature.getPickles().get(0).getSteps().get(0);
StepDefinition stepDefinition = new StubStepDefinition("I have some cukes in my belly", ItemQuantity.class);
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);
Executable testMethod = () -> stepDefinitionMatch.runStep(null);
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, sameInstance(userException));
}
use of io.cucumber.core.backend.CucumberInvocationTargetException in project cucumber-jvm by cucumber.
the class StepDefinitionMatchTest method rethrows_target_invocation_exceptions_from_data_table.
@Test
void rethrows_target_invocation_exceptions_from_data_table() {
Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + " Scenario: Test scenario\n" + " Given I have some cukes in my belly\n" + " | 3 | \n" + " | 14 | \n" + " | 15 | \n");
RuntimeException userException = new RuntimeException();
stepTypeRegistry.defineDataTableType(new DataTableType(ItemQuantity.class, (String cell) -> {
throw new CucumberInvocationTargetException(stubbedLocation, new InvocationTargetException(userException));
}));
Step step = feature.getPickles().get(0).getSteps().get(0);
StepDefinition stepDefinition = new StubStepDefinition("I have some cukes in my belly", ItemQuantity.class);
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);
Executable testMethod = () -> stepDefinitionMatch.runStep(null);
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, sameInstance(userException));
}
use of io.cucumber.core.backend.CucumberInvocationTargetException in project cucumber-jvm by cucumber.
the class StepDefinitionMatchTest method rethrows_target_invocation_exceptions_from_parameter_type.
@Test
void rethrows_target_invocation_exceptions_from_parameter_type() {
RuntimeException userException = new RuntimeException();
stepTypeRegistry.defineParameterType(new ParameterType<>("itemQuantity", "(few|some|lots of) (cukes|gherkins)", ItemQuantity.class, (String[] s) -> {
throw new CucumberInvocationTargetException(stubbedLocation, new InvocationTargetException(userException));
}));
Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + " Scenario: Test scenario\n" + " Given I have some cukes in my belly\n");
Step step = feature.getPickles().get(0).getSteps().get(0);
StepDefinition stepDefinition = new StubStepDefinition("I have {itemQuantity} in my belly", ItemQuantity.class);
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);
Executable testMethod = () -> stepDefinitionMatch.runStep(null);
RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
assertThat(actualThrown, sameInstance(userException));
}
use of io.cucumber.core.backend.CucumberInvocationTargetException in project cucumber-jvm by cucumber.
the class Java8LambdaStepDefinitionMarksCorrectStackElementTest method exception_from_step_should_be_defined_at_step_definition_class.
@Test
void exception_from_step_should_be_defined_at_step_definition_class() {
LambdaGlueRegistry.INSTANCE.set(myLambdaGlueRegistry);
new SomeLambdaStepDefs();
final StepDefinition stepDefinition = myLambdaGlueRegistry.getStepDefinition();
CucumberInvocationTargetException exception = assertThrows(CucumberInvocationTargetException.class, () -> stepDefinition.execute(new Object[0]));
assertThat(exception.getInvocationTargetExceptionCause(), new CustomTypeSafeMatcher<Throwable>("exception with matching stack trace") {
@Override
protected boolean matchesSafely(Throwable item) {
return Arrays.stream(item.getStackTrace()).filter(stepDefinition::isDefinedAt).findFirst().filter(stackTraceElement -> SomeLambdaStepDefs.class.getName().equals(stackTraceElement.getClassName())).isPresent();
}
});
}
Aggregations