Search in sources :

Example 11 with Feature

use of io.cucumber.core.gherkin.Feature in project cucumber-jvm by cucumber.

the class StepDefinitionMatchTest method throws_could_not_invoke_step_when_execution_failed_due_to_bad_methods.

@Test
void throws_could_not_invoke_step_when_execution_failed_due_to_bad_methods() {
    Feature feature = TestFeatureParser.parse("file:test.feature", "" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have a data table\n" + "       | A | \n" + "       | B | \n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have a data table", new CucumberBackendException("This exception is expected!", new IllegalAccessException()), String.class, String.class);
    List<Argument> arguments = asList(() -> "mocked table cell", () -> "mocked table cell");
    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 a data table] defined at '{stubbed location with details}'.\n" + "It appears there was a problem with the step definition.\n" + "The converted arguments types were (java.lang.String, java.lang.String)")));
}
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 12 with Feature

use of io.cucumber.core.gherkin.Feature in project cucumber-jvm by cucumber.

the class StepDefinitionMatchTest method throws_arity_mismatch_exception_when_there_are_more_parameters_than_arguments.

@Test
void throws_arity_mismatch_exception_when_there_are_more_parameters_than_arguments() {
    Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have 4 cukes in my belly\n" + "       | A | B | \n" + "       | C | D | \n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have {int} 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);
    PickleStepDefinitionMatch 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 {int} cukes in my belly] is defined with 3 parameters at '{stubbed location with details}'.\n" + "However, the gherkin step has 2 arguments:\n" + " * 4\n" + " * Table:\n" + "      | A | B |\n" + "      | C | D |\n" + "\n" + "Step text: I have 4 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 13 with Feature

use of io.cucumber.core.gherkin.Feature in project cucumber-jvm by cucumber.

the class RuntimeTest method should_call_formatter_for_scenario_outline_with_two_examples_table_and_background.

@Test
void should_call_formatter_for_scenario_outline_with_two_examples_table_and_background() {
    Feature feature = TestFeatureParser.parse("path/test.feature", "" + "Feature: feature name\n" + "  Background: background\n" + "    Given first step\n" + "  Scenario Outline: scenario outline name\n" + "    When <x> step\n" + "    Then <y> step\n" + "    Examples: examples 1 name\n" + "      |   x    |   y   |\n" + "      | second | third |\n" + "      | second | third |\n" + "    Examples: examples 2 name\n" + "      |   x    |   y   |\n" + "      | second | third |\n");
    FormatterSpy formatterSpy = new FormatterSpy();
    Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature)).withAdditionalPlugins(formatterSpy).withEventBus(new TimeServiceEventBus(fixed(EPOCH, of("UTC")), UUID::randomUUID)).withBackendSupplier(new StubBackendSupplier(new StubStepDefinition("first step"), new StubStepDefinition("second step"), new StubStepDefinition("third step"))).build().run();
    assertThat(formatterSpy.toString(), is(equalTo("" + "TestRun started\n" + "  TestCase started\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "  TestCase finished\n" + "  TestCase started\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "  TestCase finished\n" + "  TestCase started\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "  TestCase finished\n" + "TestRun finished\n")));
}
Also used : StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) UUID(java.util.UUID) Feature(io.cucumber.core.gherkin.Feature) Test(org.junit.jupiter.api.Test)

Example 14 with Feature

use of io.cucumber.core.gherkin.Feature in project cucumber-jvm by cucumber.

the class RuntimeTest method should_fail_on_event_listener_exception_when_running_in_parallel.

@Test
void should_fail_on_event_listener_exception_when_running_in_parallel() {
    Feature feature1 = TestFeatureParser.parse("path/test.feature", "" + "Feature: feature name 1\n" + "  Scenario: scenario_1 name\n" + "    Given first step\n" + "  Scenario: scenario_2 name\n" + "    Given first step\n");
    Feature feature2 = TestFeatureParser.parse("path/test2.feature", "" + "Feature: feature name 2\n" + "  Scenario: scenario_2 name\n" + "    Given first step\n");
    ConcurrentEventListener brokenEventListener = publisher -> publisher.registerHandlerFor(TestStepFinished.class, (TestStepFinished event) -> {
        throw new RuntimeException("This exception is expected");
    });
    Executable testMethod = () -> Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature1, feature2)).withAdditionalPlugins(brokenEventListener).withRuntimeOptions(new RuntimeOptionsBuilder().setThreads(2).build()).build().run();
    CompositeCucumberException actualThrown = assertThrows(CompositeCucumberException.class, testMethod);
    assertThat(actualThrown.getMessage(), is(equalTo("There were 3 exceptions. The details are in the stacktrace below.")));
    assertThat(actualThrown.getSuppressed(), is(arrayWithSize(3)));
}
Also used : ConcurrentEventListener(io.cucumber.plugin.ConcurrentEventListener) TestStepStarted(io.cucumber.plugin.event.TestStepStarted) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Meta(io.cucumber.messages.types.Meta) Status(io.cucumber.plugin.event.Status) ScenarioScoped(io.cucumber.core.backend.ScenarioScoped) Collections.singletonList(java.util.Collections.singletonList) StepDefinedEvent(io.cucumber.plugin.event.StepDefinedEvent) Arrays.asList(java.util.Arrays.asList) CompositeCucumberException(io.cucumber.core.exception.CompositeCucumberException) Is.is(org.hamcrest.core.Is.is) URI(java.net.URI) TestBackendSupplier(io.cucumber.core.runner.TestBackendSupplier) HookDefinition(io.cucumber.core.backend.HookDefinition) TestFeatureParser(io.cucumber.core.feature.TestFeatureParser) StepDurationTimeService(io.cucumber.core.runner.StepDurationTimeService) Envelope(io.cucumber.messages.types.Envelope) Result(io.cucumber.plugin.event.Result) Collections.emptyList(java.util.Collections.emptyList) Glue(io.cucumber.core.backend.Glue) UUID(java.util.UUID) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Matchers.matchesPattern(org.hamcrest.Matchers.matchesPattern) RuntimeOptionsBuilder(io.cucumber.core.options.RuntimeOptionsBuilder) TestRunFinished(io.cucumber.plugin.event.TestRunFinished) ZERO(java.time.Duration.ZERO) Mockito.mock(org.mockito.Mockito.mock) Plugin(io.cucumber.plugin.Plugin) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) StepDefinition(io.cucumber.plugin.event.StepDefinition) ZoneId.of(java.time.ZoneId.of) TestStepFinished(io.cucumber.plugin.event.TestStepFinished) TestCase(io.cucumber.plugin.event.TestCase) EventListener(io.cucumber.plugin.EventListener) ArrayList(java.util.ArrayList) TestCaseStarted(io.cucumber.plugin.event.TestCaseStarted) ArgumentCaptor(org.mockito.ArgumentCaptor) ParameterInfo(io.cucumber.core.backend.ParameterInfo) TestCaseFinished(io.cucumber.plugin.event.TestCaseFinished) TestCaseState(io.cucumber.core.backend.TestCaseState) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) EventPublisher(io.cucumber.plugin.event.EventPublisher) EventBus(io.cucumber.core.eventbus.EventBus) Clock.fixed(java.time.Clock.fixed) TestRunStarted(io.cucumber.plugin.event.TestRunStarted) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Executable(org.junit.jupiter.api.function.Executable) Feature(io.cucumber.core.gherkin.Feature) Clock(java.time.Clock) HOURS(java.util.concurrent.TimeUnit.HOURS) EPOCH(java.time.Instant.EPOCH) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestStepFinished(io.cucumber.plugin.event.TestStepFinished) CompositeCucumberException(io.cucumber.core.exception.CompositeCucumberException) RuntimeOptionsBuilder(io.cucumber.core.options.RuntimeOptionsBuilder) Executable(org.junit.jupiter.api.function.Executable) Feature(io.cucumber.core.gherkin.Feature) ConcurrentEventListener(io.cucumber.plugin.ConcurrentEventListener) Test(org.junit.jupiter.api.Test)

Example 15 with Feature

use of io.cucumber.core.gherkin.Feature in project cucumber-jvm by cucumber.

the class RuntimeTest method should_call_formatter_for_two_scenarios_with_background.

@Test
void should_call_formatter_for_two_scenarios_with_background() {
    Feature feature = TestFeatureParser.parse("path/test.feature", "" + "Feature: feature name\n" + "  Background: background\n" + "    Given first step\n" + "  Scenario: scenario_1 name\n" + "    When second step\n" + "    Then third step\n" + "  Scenario: scenario_2 name\n" + "    Then second step\n");
    FormatterSpy formatterSpy = new FormatterSpy();
    Runtime.builder().withFeatureSupplier(new StubFeatureSupplier(feature)).withAdditionalPlugins(formatterSpy).withBackendSupplier(new StubBackendSupplier(new StubStepDefinition("first step"), new StubStepDefinition("second step"), new StubStepDefinition("third step"))).build().run();
    assertThat(formatterSpy.toString(), is(equalTo("" + "TestRun started\n" + "  TestCase started\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "  TestCase finished\n" + "  TestCase started\n" + "    TestStep started\n" + "    TestStep finished\n" + "    TestStep started\n" + "    TestStep finished\n" + "  TestCase finished\n" + "TestRun finished\n")));
}
Also used : StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Feature(io.cucumber.core.gherkin.Feature) Test(org.junit.jupiter.api.Test)

Aggregations

Feature (io.cucumber.core.gherkin.Feature)152 Test (org.junit.jupiter.api.Test)144 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)92 StubBackendSupplier (io.cucumber.core.runtime.StubBackendSupplier)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)78 StubFeatureSupplier (io.cucumber.core.runtime.StubFeatureSupplier)77 UUID (java.util.UUID)67 TimeServiceEventBus (io.cucumber.core.runtime.TimeServiceEventBus)59 StubHookDefinition (io.cucumber.core.backend.StubHookDefinition)26 RuntimeOptionsBuilder (io.cucumber.core.options.RuntimeOptionsBuilder)23 Step (io.cucumber.core.gherkin.Step)22 StepDefinition (io.cucumber.core.backend.StepDefinition)21 StepDurationTimeService (io.cucumber.core.runner.StepDurationTimeService)21 Argument (io.cucumber.core.stepexpression.Argument)18 StepExpression (io.cucumber.core.stepexpression.StepExpression)18 DocString (io.cucumber.docstring.DocString)17 Executable (org.junit.jupiter.api.function.Executable)16 URI (java.net.URI)15 Arrays.asList (java.util.Arrays.asList)14 Collections.singletonList (java.util.Collections.singletonList)13