Search in sources :

Example 16 with CucumberException

use of io.cucumber.core.exception.CucumberException in project cucumber-jvm by cucumber.

the class TestNGFormatter method handleTestRunFinished.

private void handleTestRunFinished(TestRunFinished event) {
    try {
        Instant finished = event.getInstant();
        Duration duration = Duration.between(started, finished);
        results.setAttribute("total", String.valueOf(getElementsCountByAttribute(suite, "status", ".*")));
        results.setAttribute("passed", String.valueOf(getElementsCountByAttribute(suite, "status", "PASS")));
        results.setAttribute("failed", String.valueOf(getElementsCountByAttribute(suite, "status", "FAIL")));
        results.setAttribute("skipped", String.valueOf(getElementsCountByAttribute(suite, "status", "SKIP")));
        suite.setAttribute("name", TestNGFormatter.class.getName());
        suite.setAttribute("duration-ms", String.valueOf(duration.toMillis()));
        test.setAttribute("name", TestNGFormatter.class.getName());
        test.setAttribute("duration-ms", String.valueOf(duration.toMillis()));
        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult streamResult = new StreamResult(writer);
        DOMSource domSource = new DOMSource(document);
        transformer.transform(domSource, streamResult);
        closeQuietly(writer);
    } catch (TransformerException e) {
        throw new CucumberException("Error transforming report.", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Instant(java.time.Instant) Duration(java.time.Duration) CucumberException(io.cucumber.core.exception.CucumberException) TransformerException(javax.xml.transform.TransformerException)

Example 17 with CucumberException

use of io.cucumber.core.exception.CucumberException in project cucumber-jvm by cucumber.

the class EncodingParser method read.

private static String read(Resource resource, String encoding) {
    char[] buffer = new char[2 * 1024];
    final StringBuilder out = new StringBuilder();
    try (InputStream is = resource.getInputStream();
        InputStreamReader in = new InputStreamReader(is, encoding);
        BufferedReader reader = new BufferedReader(in)) {
        int read;
        while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
            out.append(buffer, 0, read);
        }
    } catch (IOException e) {
        throw new CucumberException("Failed to read resource:" + resource.getUri(), e);
    }
    return out.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) CucumberException(io.cucumber.core.exception.CucumberException)

Example 18 with CucumberException

use of io.cucumber.core.exception.CucumberException in project cucumber-jvm by cucumber.

the class StepDefinitionMatchTest method throws_could_not_invoke_step_when_execution_failed_with_null_arguments.

@Test
void throws_could_not_invoke_step_when_execution_failed_with_null_arguments() {
    Feature feature = TestFeatureParser.parse("file:test.feature", "" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have an null value\n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have an {word} value", new CucumberBackendException("This exception is expected!", new IllegalAccessException()), String.class);
    List<Argument> arguments = asList(() -> null);
    StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition, URI.create("file:path/to.feature"), step);
    Executable testMethod = () -> stepDefinitionMatch.runStep(null);
    CucumberException actualThrown = assertThrows(CucumberException.class, testMethod);
    assertThat("Unexpected exception message", actualThrown.getMessage(), is(equalTo("Could not invoke step [I have an {word} value] defined at '{stubbed location with details}'.\n" + "It appears there was a problem with the step definition.\n" + "The converted arguments types were (null)")));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) CucumberBackendException(io.cucumber.core.backend.CucumberBackendException) Step(io.cucumber.core.gherkin.Step) CucumberException(io.cucumber.core.exception.CucumberException) Executable(org.junit.jupiter.api.function.Executable) Feature(io.cucumber.core.gherkin.Feature) Test(org.junit.jupiter.api.Test)

Example 19 with CucumberException

use of io.cucumber.core.exception.CucumberException in project cucumber-jvm by cucumber.

the class StepDefinitionMatchTest method throws_arity_mismatch_exception_when_there_are_more_parameters_and_no_arguments.

@Test
void throws_arity_mismatch_exception_when_there_are_more_parameters_and_no_arguments() {
    Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have cukes in my belly\n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have cukes in my belly", Integer.TYPE, Short.TYPE, List.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);
    CucumberException actualThrown = assertThrows(CucumberException.class, testMethod);
    assertThat("Unexpected exception message", actualThrown.getMessage(), is(equalTo("Step [I have cukes in my belly] is defined with 3 parameters at '{stubbed location with details}'.\n" + "However, the gherkin step has 0 arguments.\n" + "Step text: I have cukes in my belly")));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Step(io.cucumber.core.gherkin.Step) CucumberException(io.cucumber.core.exception.CucumberException) Executable(org.junit.jupiter.api.function.Executable) Feature(io.cucumber.core.gherkin.Feature) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 20 with CucumberException

use of io.cucumber.core.exception.CucumberException in project cucumber-jvm by cucumber.

the class StepDefinitionMatchTest method throws_register_type_in_configuration_exception_when_there_is_no_data_table_type_defined.

@Test
void throws_register_type_in_configuration_exception_when_there_is_no_data_table_type_defined() {
    Feature feature = TestFeatureParser.parse("file:test.feature", "" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have a data table\n" + "       | A | \n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have a data table", UndefinedDataTableType.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, URI.create("file:path/to.feature"), step);
    Executable testMethod = () -> stepDefinitionMatch.runStep(null);
    CucumberException actualThrown = assertThrows(CucumberException.class, testMethod);
    assertThat("Unexpected exception message", actualThrown.getMessage(), is(equalTo("Could not convert arguments for step [I have a data table] defined at '{stubbed location with details}'.\n" + "It appears you did not register a data table type.")));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Step(io.cucumber.core.gherkin.Step) CucumberException(io.cucumber.core.exception.CucumberException) Executable(org.junit.jupiter.api.function.Executable) Feature(io.cucumber.core.gherkin.Feature) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Aggregations

CucumberException (io.cucumber.core.exception.CucumberException)39 Test (org.junit.jupiter.api.Test)28 Executable (org.junit.jupiter.api.function.Executable)19 StepDefinition (io.cucumber.core.backend.StepDefinition)12 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)12 Feature (io.cucumber.core.gherkin.Feature)11 Step (io.cucumber.core.gherkin.Step)11 Argument (io.cucumber.core.stepexpression.Argument)11 StepExpression (io.cucumber.core.stepexpression.StepExpression)8 PluginOption (io.cucumber.core.options.PluginOption)5 CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)4 IOException (java.io.IOException)3 URI (java.net.URI)3 Options (io.cucumber.core.backend.Options)2 DataTableType (io.cucumber.datatable.DataTableType)2 File (java.io.File)2 InputStream (java.io.InputStream)2 URLClassLoader (java.net.URLClassLoader)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2