Search in sources :

Example 1 with ObjectFactory

use of io.cucumber.core.backend.ObjectFactory 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 ObjectFactory

use of io.cucumber.core.backend.ObjectFactory 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 ObjectFactory

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

the class RunnerTest method backends_are_asked_for_snippets_for_undefined_steps.

@Test
void backends_are_asked_for_snippets_for_undefined_steps() {
    Backend backend = mock(Backend.class);
    when(backend.getSnippet()).thenReturn(new TestSnippet());
    ObjectFactory objectFactory = mock(ObjectFactory.class);
    Runner runner = new Runner(bus, singletonList(backend), objectFactory, runtimeOptions);
    runner.runPickle(createPicklesWithSteps());
    verify(backend).getSnippet();
}
Also used : Backend(io.cucumber.core.backend.Backend) ObjectFactory(io.cucumber.core.backend.ObjectFactory) TestSnippet(io.cucumber.core.snippets.TestSnippet) Test(org.junit.jupiter.api.Test)

Example 4 with ObjectFactory

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

the class SpringFactoryTest method shouldThrowWhenGlueScopedSpringBeanAreUsedOutsideLifecycle.

@Test
void shouldThrowWhenGlueScopedSpringBeanAreUsedOutsideLifecycle() {
    final ObjectFactory factory = new SpringFactory();
    factory.addClass(WithSpringAnnotations.class);
    factory.start();
    final Belly belly = factory.getInstance(Belly.class);
    final GlueScopedComponent glue = factory.getInstance(GlueScopedComponent.class);
    factory.stop();
    assertDoesNotThrow(belly::getInstanceId);
    assertThrows(BeanCreationException.class, glue::getInstanceId);
}
Also used : GlueScopedComponent(io.cucumber.spring.beans.GlueScopedComponent) ObjectFactory(io.cucumber.core.backend.ObjectFactory) Belly(io.cucumber.spring.beans.Belly) Test(org.junit.jupiter.api.Test)

Example 5 with ObjectFactory

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

the class SpringFactoryTest method shouldGiveUsNewStepInstancesForEachScenario.

@Test
void shouldGiveUsNewStepInstancesForEachScenario() {
    final ObjectFactory factory = new SpringFactory();
    factory.addClass(BellyStepDefinitions.class);
    // Scenario 1
    factory.start();
    final BellyStepDefinitions o1 = factory.getInstance(BellyStepDefinitions.class);
    factory.stop();
    // Scenario 2
    factory.start();
    final BellyStepDefinitions o2 = factory.getInstance(BellyStepDefinitions.class);
    factory.stop();
    assertAll(() -> assertThat(o1, is(notNullValue())), () -> assertThat(o2, is(notNullValue())), () -> assertThat(o1, is(not(equalTo(o2)))), () -> assertThat(o2, is(not(equalTo(o1)))));
}
Also used : DirtiesContextBellyStepDefinitions(io.cucumber.spring.dirtiescontextconfig.DirtiesContextBellyStepDefinitions) BellyStepDefinitions(io.cucumber.spring.contextconfig.BellyStepDefinitions) ObjectFactory(io.cucumber.core.backend.ObjectFactory) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectFactory (io.cucumber.core.backend.ObjectFactory)29 Test (org.junit.jupiter.api.Test)27 Backend (io.cucumber.core.backend.Backend)5 TestSnippet (io.cucumber.core.snippets.TestSnippet)4 BellyBean (io.cucumber.spring.beans.BellyBean)4 CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)3 Glue (io.cucumber.core.backend.Glue)3 HookDefinition (io.cucumber.core.backend.HookDefinition)3 DirtiesContextBellyStepDefinitions (io.cucumber.spring.dirtiescontextconfig.DirtiesContextBellyStepDefinitions)3 Executable (org.junit.jupiter.api.function.Executable)3 Belly (io.cucumber.spring.beans.Belly)2 GlueScopedComponent (io.cucumber.spring.beans.GlueScopedComponent)2 AutowiresThirdStepDef (io.cucumber.spring.commonglue.AutowiresThirdStepDef)2 OneStepDef (io.cucumber.spring.commonglue.OneStepDef)2 BellyStepDefinitions (io.cucumber.spring.contextconfig.BellyStepDefinitions)2 DirtiesContextBellyMetaStepDefinitions (io.cucumber.spring.metaconfig.dirties.DirtiesContextBellyMetaStepDefinitions)2 InOrder (org.mockito.InOrder)2 BackendProviderService (io.cucumber.core.backend.BackendProviderService)1 DefaultObjectFactory (io.cucumber.core.backend.DefaultObjectFactory)1 StaticHookDefinition (io.cucumber.core.backend.StaticHookDefinition)1