Search in sources :

Example 6 with HookDefinition

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

the class HookOrderTest method hooks_order_across_many_backends.

@Test
void hooks_order_across_many_backends() {
    final List<HookDefinition> backend1Hooks = mockHooks(3, Integer.MAX_VALUE, 1);
    final List<HookDefinition> backend2Hooks = mockHooks(2, Integer.MAX_VALUE, 4);
    TestRunnerSupplier runnerSupplier = new TestRunnerSupplier(bus, runtimeOptions) {

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

Example 7 with HookDefinition

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

the class HookOrderTest method before_step_hooks_execute_in_order.

@Test
void before_step_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(stepDefinition);
            for (HookDefinition hook : hooks) {
                glue.addBeforeStepHook(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) ArrayList(java.util.ArrayList) List(java.util.List) HookDefinition(io.cucumber.core.backend.HookDefinition) Test(org.junit.jupiter.api.Test)

Example 8 with HookDefinition

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

the class HookOrderTest method mockHooks.

private List<HookDefinition> mockHooks(int... ordering) {
    List<HookDefinition> hooks = new ArrayList<>();
    for (int order : ordering) {
        HookDefinition hook = mock(HookDefinition.class, "Mock number " + order);
        when(hook.getOrder()).thenReturn(order);
        when(hook.getTagExpression()).thenReturn("");
        when(hook.getLocation()).thenReturn("Mock location");
        hooks.add(hook);
    }
    return hooks;
}
Also used : ArrayList(java.util.ArrayList) HookDefinition(io.cucumber.core.backend.HookDefinition)

Example 9 with HookDefinition

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

the class RunnerTest method hooks_execute_also_after_failure.

@Test
void hooks_execute_also_after_failure() {
    HookDefinition beforeHook = createHook();
    HookDefinition afterHook = createHook();
    HookDefinition failingBeforeHook = createHook();
    doThrow(new RuntimeException("boom")).when(failingBeforeHook).execute(any(TestCaseState.class));
    TestRunnerSupplier runnerSupplier = new TestRunnerSupplier(bus, runtimeOptions) {

        @Override
        public void loadGlue(Glue glue, List<URI> gluePaths) {
            glue.addBeforeHook(failingBeforeHook);
            glue.addBeforeHook(beforeHook);
            glue.addAfterHook(afterHook);
        }
    };
    runnerSupplier.get().runPickle(createPicklesWithSteps());
    InOrder inOrder = inOrder(failingBeforeHook, beforeHook, afterHook);
    inOrder.verify(failingBeforeHook).execute(any(TestCaseState.class));
    inOrder.verify(beforeHook).execute(any(TestCaseState.class));
    inOrder.verify(afterHook).execute(any(TestCaseState.class));
}
Also used : InOrder(org.mockito.InOrder) 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 10 with HookDefinition

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

the class HookTest method after_hooks_execute_before_objects_are_disposed.

/**
 * Test for
 * <a href="https://github.com/cucumber/cucumber-jvm/issues/23">#23</a>.
 */
@Test
void after_hooks_execute_before_objects_are_disposed() {
    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());
    Runner runner = new Runner(bus, Collections.singleton(backend), objectFactory, runtimeOptions);
    runner.runPickle(pickle);
    InOrder inOrder = inOrder(hook, backend);
    inOrder.verify(backend).buildWorld();
    inOrder.verify(hook).execute(ArgumentMatchers.any());
    inOrder.verify(backend).disposeWorld();
}
Also used : Backend(io.cucumber.core.backend.Backend) InOrder(org.mockito.InOrder) 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)

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