Search in sources :

Example 1 with StaticHookDefinition

use of io.cucumber.core.backend.StaticHookDefinition 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 2 with StaticHookDefinition

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

the class RunnerTest method all_static_hooks_execute_also_after_failure.

@Test
void all_static_hooks_execute_also_after_failure() {
    StaticHookDefinition beforeAllHook = createStaticHook();
    StaticHookDefinition failingBeforeAllHook = createStaticHook();
    doThrow(new RuntimeException("boom")).when(failingBeforeAllHook).execute();
    TestRunnerSupplier runnerSupplier = new TestRunnerSupplier(bus, runtimeOptions) {

        @Override
        public void loadGlue(Glue glue, List<URI> gluePaths) {
            glue.addBeforeAllHook(beforeAllHook);
            glue.addBeforeAllHook(failingBeforeAllHook);
        }
    };
    Runner runner = runnerSupplier.get();
    assertThrows(RuntimeException.class, runner::runBeforeAllHooks);
    InOrder inOrder = inOrder(beforeAllHook, failingBeforeAllHook);
    inOrder.verify(beforeAllHook).execute();
    inOrder.verify(failingBeforeAllHook).execute();
}
Also used : InOrder(org.mockito.InOrder) Glue(io.cucumber.core.backend.Glue) StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 3 with StaticHookDefinition

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

the class RunnerTest method createStaticHook.

private StaticHookDefinition createStaticHook() {
    StaticHookDefinition hook = mock(StaticHookDefinition.class);
    when(hook.getLocation()).thenReturn("");
    return hook;
}
Also used : StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition)

Example 4 with StaticHookDefinition

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

the class RunnerTest method hooks_not_executed_in_dry_run_mode.

@Test
void hooks_not_executed_in_dry_run_mode() {
    RuntimeOptions runtimeOptions = new RuntimeOptionsBuilder().setDryRun().build();
    StaticHookDefinition beforeAllHook = createStaticHook();
    StaticHookDefinition afterAllHook = createStaticHook();
    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.addBeforeAllHook(beforeAllHook);
            glue.addAfterAllHook(afterAllHook);
            glue.addBeforeHook(beforeHook);
            glue.addAfterHook(afterHook);
            glue.addBeforeStepHook(beforeStepHook);
            glue.addAfterStepHook(afterStepHook);
        }
    };
    runnerSupplier.get().runBeforeAllHooks();
    runnerSupplier.get().runPickle(createPicklesWithSteps());
    runnerSupplier.get().runAfterAllHooks();
    verify(beforeAllHook, never()).execute();
    verify(afterAllHook, never()).execute();
    verify(beforeHook, never()).execute(any(TestCaseState.class));
    verify(afterHook, never()).execute(any(TestCaseState.class));
    verify(beforeStepHook, never()).execute(any(TestCaseState.class));
    verify(afterStepHook, never()).execute(any(TestCaseState.class));
}
Also used : Glue(io.cucumber.core.backend.Glue) RuntimeOptionsBuilder(io.cucumber.core.options.RuntimeOptionsBuilder) StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) RuntimeOptions(io.cucumber.core.options.RuntimeOptions) StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition) HookDefinition(io.cucumber.core.backend.HookDefinition) Test(org.junit.jupiter.api.Test)

Example 5 with StaticHookDefinition

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

the class Runner method executeHooks.

private void executeHooks(List<StaticHookDefinition> afterAllHooks) {
    ThrowableCollector throwableCollector = new ThrowableCollector();
    for (StaticHookDefinition staticHookDefinition : afterAllHooks) {
        throwableCollector.execute(() -> executeHook(staticHookDefinition));
    }
    Throwable throwable = throwableCollector.getThrowable();
    if (throwable != null) {
        throwAsUncheckedException(throwable);
    }
}
Also used : StaticHookDefinition(io.cucumber.core.backend.StaticHookDefinition)

Aggregations

StaticHookDefinition (io.cucumber.core.backend.StaticHookDefinition)5 Glue (io.cucumber.core.backend.Glue)3 Test (org.junit.jupiter.api.Test)3 HookDefinition (io.cucumber.core.backend.HookDefinition)2 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 List (java.util.List)2 InOrder (org.mockito.InOrder)2 Backend (io.cucumber.core.backend.Backend)1 ObjectFactory (io.cucumber.core.backend.ObjectFactory)1 RuntimeOptions (io.cucumber.core.options.RuntimeOptions)1 RuntimeOptionsBuilder (io.cucumber.core.options.RuntimeOptionsBuilder)1 TestSnippet (io.cucumber.core.snippets.TestSnippet)1