Search in sources :

Example 1 with CucumberInvocationTargetException

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())));
}
Also used : CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 2 with CucumberInvocationTargetException

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));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Step(io.cucumber.core.gherkin.Step) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) DataTableType(io.cucumber.datatable.DataTableType) Located(io.cucumber.core.backend.Located) StepDefinition(io.cucumber.core.backend.StepDefinition) StepExpression(io.cucumber.core.stepexpression.StepExpression) Arrays.asList(java.util.Arrays.asList) Is.is(org.hamcrest.core.Is.is) URI(java.net.URI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ENGLISH(java.util.Locale.ENGLISH) EventBus(io.cucumber.core.eventbus.EventBus) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) TestFeatureParser(io.cucumber.core.feature.TestFeatureParser) StepTypeRegistry(io.cucumber.core.stepexpression.StepTypeRegistry) StepExpressionFactory(io.cucumber.core.stepexpression.StepExpressionFactory) TimeServiceEventBus(io.cucumber.core.runtime.TimeServiceEventBus) UUID(java.util.UUID) InvocationTargetException(java.lang.reflect.InvocationTargetException) CucumberBackendException(io.cucumber.core.backend.CucumberBackendException) Test(org.junit.jupiter.api.Test) List(java.util.List) Executable(org.junit.jupiter.api.function.Executable) Feature(io.cucumber.core.gherkin.Feature) Argument(io.cucumber.core.stepexpression.Argument) DocStringType(io.cucumber.docstring.DocStringType) Clock(java.time.Clock) Collections(java.util.Collections) CucumberException(io.cucumber.core.exception.CucumberException) ParameterType(io.cucumber.cucumberexpressions.ParameterType) Argument(io.cucumber.core.stepexpression.Argument) Step(io.cucumber.core.gherkin.Step) Feature(io.cucumber.core.gherkin.Feature) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DocStringType(io.cucumber.docstring.DocStringType) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Executable(org.junit.jupiter.api.function.Executable) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 3 with CucumberInvocationTargetException

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));
}
Also used : DataTableType(io.cucumber.datatable.DataTableType) Argument(io.cucumber.core.stepexpression.Argument) Step(io.cucumber.core.gherkin.Step) Feature(io.cucumber.core.gherkin.Feature) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Executable(org.junit.jupiter.api.function.Executable) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 4 with CucumberInvocationTargetException

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));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) Step(io.cucumber.core.gherkin.Step) Feature(io.cucumber.core.gherkin.Feature) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Executable(org.junit.jupiter.api.function.Executable) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 5 with CucumberInvocationTargetException

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();
        }
    });
}
Also used : CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) StepDefinition(io.cucumber.core.backend.StepDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

CucumberInvocationTargetException (io.cucumber.core.backend.CucumberInvocationTargetException)6 Test (org.junit.jupiter.api.Test)5 StepDefinition (io.cucumber.core.backend.StepDefinition)4 Argument (io.cucumber.core.stepexpression.Argument)4 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)3 Feature (io.cucumber.core.gherkin.Feature)3 Step (io.cucumber.core.gherkin.Step)3 StepExpression (io.cucumber.core.stepexpression.StepExpression)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Executable (org.junit.jupiter.api.function.Executable)3 CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)2 DataTableType (io.cucumber.datatable.DataTableType)2 Located (io.cucumber.core.backend.Located)1 ParameterInfo (io.cucumber.core.backend.ParameterInfo)1 EventBus (io.cucumber.core.eventbus.EventBus)1 CucumberException (io.cucumber.core.exception.CucumberException)1 TestFeatureParser (io.cucumber.core.feature.TestFeatureParser)1 TimeServiceEventBus (io.cucumber.core.runtime.TimeServiceEventBus)1 StepExpressionFactory (io.cucumber.core.stepexpression.StepExpressionFactory)1 StepTypeRegistry (io.cucumber.core.stepexpression.StepTypeRegistry)1