Search in sources :

Example 1 with HookDefinition

use of io.cucumber.core.backend.HookDefinition in project cucumber-jvm by cucumber.

the class HookTest method hook_throws_exception_with_name_when_tag_expression_is_invalid.

@Test
void hook_throws_exception_with_name_when_tag_expression_is_invalid() {
    Backend backend = mock(Backend.class);
    when(backend.getSnippet()).thenReturn(new TestSnippet());
    ObjectFactory objectFactory = mock(ObjectFactory.class);
    final HookDefinition hook = mock(HookDefinition.class);
    when(hook.getLocation()).thenReturn("hook-location");
    when(hook.getTagExpression()).thenReturn("(");
    doAnswer(invocation -> {
        Glue glue = invocation.getArgument(0);
        glue.addBeforeHook(hook);
        return null;
    }).when(backend).loadGlue(any(Glue.class), ArgumentMatchers.anyList());
    RuntimeException e = assertThrows(RuntimeException.class, () -> new Runner(bus, Collections.singleton(backend), objectFactory, runtimeOptions));
    assertThat(e.getMessage(), is("Invalid tag expression at 'hook-location'"));
}
Also used : Backend(io.cucumber.core.backend.Backend) ObjectFactory(io.cucumber.core.backend.ObjectFactory) Glue(io.cucumber.core.backend.Glue) TestSnippet(io.cucumber.core.snippets.TestSnippet) HookDefinition(io.cucumber.core.backend.HookDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with HookDefinition

use of io.cucumber.core.backend.HookDefinition in project cucumber-jvm by cucumber.

the class RunnerTest method hooks_execute_inside_world_and_around_world.

@Test
void hooks_execute_inside_world_and_around_world() {
    StaticHookDefinition beforeAllHook = createStaticHook();
    StaticHookDefinition afterAllHook = createStaticHook();
    HookDefinition beforeHook = createHook();
    HookDefinition afterHook = createHook();
    Backend backend = mock(Backend.class);
    when(backend.getSnippet()).thenReturn(new TestSnippet());
    ObjectFactory objectFactory = mock(ObjectFactory.class);
    doAnswer(invocation -> {
        Glue glue = invocation.getArgument(0);
        glue.addBeforeAllHook(beforeAllHook);
        glue.addAfterAllHook(afterAllHook);
        glue.addBeforeHook(beforeHook);
        glue.addAfterHook(afterHook);
        return null;
    }).when(backend).loadGlue(any(Glue.class), ArgumentMatchers.anyList());
    Runner runner = new Runner(bus, singletonList(backend), objectFactory, runtimeOptions);
    runner.runBeforeAllHooks();
    runner.runPickle(createPicklesWithSteps());
    runner.runAfterAllHooks();
    InOrder inOrder = inOrder(beforeAllHook, afterAllHook, beforeHook, afterHook, backend);
    inOrder.verify(beforeAllHook).execute();
    inOrder.verify(backend).buildWorld();
    inOrder.verify(beforeHook).execute(any(TestCaseState.class));
    inOrder.verify(afterHook).execute(any(TestCaseState.class));
    inOrder.verify(backend).disposeWorld();
    inOrder.verify(afterAllHook).execute();
}
Also used : Backend(io.cucumber.core.backend.Backend) InOrder(org.mockito.InOrder) ObjectFactory(io.cucumber.core.backend.ObjectFactory) Glue(io.cucumber.core.backend.Glue) StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition) TestSnippet(io.cucumber.core.snippets.TestSnippet) StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition) HookDefinition(io.cucumber.core.backend.HookDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with HookDefinition

use of io.cucumber.core.backend.HookDefinition in project cucumber-jvm by cucumber.

the class RunnerTest method scenario_hooks_not_executed_for_empty_pickles.

@Test
void scenario_hooks_not_executed_for_empty_pickles() {
    HookDefinition beforeHook = createHook();
    HookDefinition afterHook = createHook();
    HookDefinition beforeStepHook = createHook();
    HookDefinition afterStepHook = createHook();
    TestRunnerSupplier runnerSupplier = new TestRunnerSupplier(bus, runtimeOptions) {

        @Override
        public void loadGlue(Glue glue, List<URI> gluePaths) {
            glue.addBeforeHook(beforeHook);
            glue.addAfterHook(afterHook);
            glue.addBeforeStepHook(beforeStepHook);
            glue.addAfterStepHook(afterStepHook);
        }
    };
    runnerSupplier.get().runPickle(createEmptyPickle());
    verify(beforeHook, never()).execute(any(TestCaseState.class));
    verify(afterStepHook, never()).execute(any(TestCaseState.class));
    verify(afterHook, never()).execute(any(TestCaseState.class));
}
Also used : Glue(io.cucumber.core.backend.Glue) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition) HookDefinition(io.cucumber.core.backend.HookDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with HookDefinition

use of io.cucumber.core.backend.HookDefinition in project cucumber-jvm by cucumber.

the class CachingGlueTest method sorts_before_hooks_by_order.

@Test
void sorts_before_hooks_by_order() {
    HookDefinition hookDefinition1 = new MockedScenarioScopedHookDefinition(12);
    HookDefinition hookDefinition2 = new MockedScenarioScopedHookDefinition(13);
    HookDefinition hookDefinition3 = new MockedScenarioScopedHookDefinition(24);
    glue.addBeforeHook(hookDefinition1);
    glue.addBeforeHook(hookDefinition2);
    glue.addBeforeHook(hookDefinition3);
    List<HookDefinition> hooks = glue.getBeforeHooks().stream().map(CoreHookDefinition::getDelegate).collect(Collectors.toList());
    assertThat(hooks, contains(hookDefinition1, hookDefinition2, hookDefinition3));
}
Also used : HookDefinition(io.cucumber.core.backend.HookDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with HookDefinition

use of io.cucumber.core.backend.HookDefinition in project cucumber-jvm by cucumber.

the class HookOrderTest method before_hooks_execute_in_order.

@Test
void before_hooks_execute_in_order() {
    final List<HookDefinition> hooks = mockHooks(3, Integer.MAX_VALUE, 1, -1, 0, 10000, Integer.MIN_VALUE);
    TestRunnerSupplier runnerSupplier = new TestRunnerSupplier(bus, runtimeOptions) {

        @Override
        public void loadGlue(Glue glue, List<URI> gluePaths) {
            glue.addStepDefinition(new StubStepDefinition("pattern1"));
            for (HookDefinition hook : hooks) {
                glue.addBeforeHook(hook);
            }
        }
    };
    runnerSupplier.get().runPickle(pickle);
    InOrder inOrder = inOrder(hooks.toArray());
    inOrder.verify(hooks.get(6)).execute(ArgumentMatchers.any());
    inOrder.verify(hooks.get(3)).execute(ArgumentMatchers.any());
    inOrder.verify(hooks.get(4)).execute(ArgumentMatchers.any());
    inOrder.verify(hooks.get(2)).execute(ArgumentMatchers.any());
    inOrder.verify(hooks.get(0)).execute(ArgumentMatchers.any());
    inOrder.verify(hooks.get(5)).execute(ArgumentMatchers.any());
    inOrder.verify(hooks.get(1)).execute(ArgumentMatchers.any());
}
Also used : InOrder(org.mockito.InOrder) Glue(io.cucumber.core.backend.Glue) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ArrayList(java.util.ArrayList) List(java.util.List) HookDefinition(io.cucumber.core.backend.HookDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

HookDefinition (io.cucumber.core.backend.HookDefinition)20 Test (org.junit.jupiter.api.Test)18 Glue (io.cucumber.core.backend.Glue)14 List (java.util.List)11 InOrder (org.mockito.InOrder)11 StaticHookDefinition (io.cucumber.core.backend.StaticHookDefinition)8 ArrayList (java.util.ArrayList)6 Collections.emptyList (java.util.Collections.emptyList)6 Collections.singletonList (java.util.Collections.singletonList)6 Backend (io.cucumber.core.backend.Backend)3 ObjectFactory (io.cucumber.core.backend.ObjectFactory)3 Pickle (io.cucumber.core.gherkin.Pickle)3 TestSnippet (io.cucumber.core.snippets.TestSnippet)3 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)2 TestCaseState (io.cucumber.core.backend.TestCaseState)1 Feature (io.cucumber.core.gherkin.Feature)1 RuntimeOptions (io.cucumber.core.options.RuntimeOptions)1 RuntimeOptionsBuilder (io.cucumber.core.options.RuntimeOptionsBuilder)1