Search in sources :

Example 6 with Configuration

use of org.jbehave.core.configuration.Configuration in project jbehave-core by jbehave.

the class EmbedderBehaviour method shouldRunStoriesAsEmbeddables.

@Test
public void shouldRunStoriesAsEmbeddables() throws Throwable {
    // Given
    PerformableTree performableTree = mock(PerformableTree.class);
    EmbedderControls embedderControls = new EmbedderControls();
    OutputStream out = new ByteArrayOutputStream();
    EmbedderMonitor monitor = new PrintStreamEmbedderMonitor(new PrintStream(out));
    String myEmbeddableName = MyEmbeddable.class.getName();
    String myOtherEmbeddableName = MyOtherEmbeddable.class.getName();
    List<String> classNames = asList(myEmbeddableName, myOtherEmbeddableName);
    Embeddable myEmbeddable = new MyEmbeddable();
    Embeddable myOtherEmbeddable = new MyOtherEmbeddable();
    List<Embeddable> embeddables = asList(myEmbeddable, myOtherEmbeddable);
    EmbedderClassLoader classLoader = mock(EmbedderClassLoader.class);
    when(classLoader.newInstance(Embeddable.class, myEmbeddableName)).thenReturn(myEmbeddable);
    when(classLoader.newInstance(Embeddable.class, myOtherEmbeddableName)).thenReturn(myOtherEmbeddable);
    // When
    Configuration configuration = new MostUsefulConfiguration();
    CandidateSteps steps = mock(CandidateSteps.class);
    Embedder embedder = embedderWith(performableTree, embedderControls, monitor);
    embedder.useClassLoader(classLoader);
    embedder.useConfiguration(configuration);
    embedder.useCandidateSteps(asList(steps));
    embedder.runAsEmbeddables(classNames);
    // Then
    for (Embeddable embeddable : embeddables) {
        assertThat(out.toString(), containsString("Running embeddable " + embeddable.getClass().getName()));
    }
}
Also used : PrintStream(java.io.PrintStream) Configuration(org.jbehave.core.configuration.Configuration) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) InjectableEmbedder(org.jbehave.core.InjectableEmbedder) UsingEmbedder(org.jbehave.core.annotations.UsingEmbedder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Embeddable(org.jbehave.core.Embeddable) CandidateSteps(org.jbehave.core.steps.CandidateSteps) Test(org.junit.Test)

Example 7 with Configuration

use of org.jbehave.core.configuration.Configuration in project jbehave-core by jbehave.

the class EmbedderBehaviour method shouldNotThrowExceptionUponFailingStoriesAsPathsIfIgnoreFailureInStoriesFlagIsSet.

@SuppressWarnings("unchecked")
@Test
public void shouldNotThrowExceptionUponFailingStoriesAsPathsIfIgnoreFailureInStoriesFlagIsSet() throws Throwable {
    // Given
    PerformableTree performableTree = mock(PerformableTree.class);
    EmbedderControls embedderControls = new EmbedderControls().doIgnoreFailureInStories(true);
    OutputStream out = new ByteArrayOutputStream();
    EmbedderMonitor monitor = new PrintStreamEmbedderMonitor(new PrintStream(out));
    List<? extends Class<? extends Embeddable>> embeddables = asList(MyStory.class, MyOtherEmbeddable.class);
    Embedder embedder = embedderWith(performableTree, embedderControls, monitor);
    Configuration configuration = embedder.configuration();
    InjectableStepsFactory stepsFactory = embedder.stepsFactory();
    MetaFilter filter = embedder.metaFilter();
    StoryPathResolver resolver = configuration.storyPathResolver();
    List<String> storyPaths = new ArrayList<>();
    Map<String, Story> stories = new HashMap<>();
    for (Class<? extends Embeddable> embeddable : embeddables) {
        String storyPath = resolver.resolve(embeddable);
        storyPaths.add(storyPath);
        Story story = mockStory(Meta.EMPTY);
        stories.put(storyPath, story);
        when(performableTree.storyOfPath(configuration, storyPath)).thenReturn(story);
        when(story.getPath()).thenReturn(storyPath);
    }
    RunContext runContext = new RunContext(configuration, stepsFactory, monitor, filter, new BatchFailures());
    when(performableTree.newRunContext(isA(Configuration.class), isA(InjectableStepsFactory.class), isA(EmbedderMonitor.class), isA(MetaFilter.class), isA(BatchFailures.class))).thenReturn(runContext);
    for (String storyPath : storyPaths) {
        doThrow(new RuntimeException(storyPath + " failed")).when(performableTree).perform(runContext, stories.get(storyPath));
    }
    // When
    embedder.runStoriesAsPaths(storyPaths);
    TimeUnit.SECONDS.sleep(2);
    // Then
    for (String storyPath : storyPaths) {
        assertThat(out.toString(), containsString("Running story " + storyPath));
        assertThat(out.toString(), containsString("Failed to run story " + storyPath));
    }
}
Also used : PrintStream(java.io.PrintStream) InjectableStepsFactory(org.jbehave.core.steps.InjectableStepsFactory) StoryPathResolver(org.jbehave.core.io.StoryPathResolver) Configuration(org.jbehave.core.configuration.Configuration) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) InjectableEmbedder(org.jbehave.core.InjectableEmbedder) UsingEmbedder(org.jbehave.core.annotations.UsingEmbedder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) BatchFailures(org.jbehave.core.failures.BatchFailures) RunContext(org.jbehave.core.embedder.PerformableTree.RunContext) JUnitStory(org.jbehave.core.junit.JUnitStory) Story(org.jbehave.core.model.Story) Test(org.junit.Test)

Example 8 with Configuration

use of org.jbehave.core.configuration.Configuration in project jbehave-core by jbehave.

the class StoryRunnerBehaviour method shouldRunAfterAndBeforeScenarioSteps.

@Test
public void shouldRunAfterAndBeforeScenarioSteps() throws Throwable {
    // Given
    Scenario scenario1 = new Scenario("my title 1", Meta.EMPTY, GivenStories.EMPTY, ExamplesTable.EMPTY, asList("step"));
    Story story = new Story(new Description("my blurb"), Narrative.EMPTY, asList(scenario1));
    StoryReporter reporter = mock(ConcurrentStoryReporter.class);
    StepCollector collector = mock(StepCollector.class);
    FailureStrategy failureStrategy = mock(FailureStrategy.class);
    Configuration configuration = configurationWith(reporter, collector, failureStrategy);
    configuration.storyControls().doDryRun(true);
    CandidateSteps mySteps = new Steps(configuration);
    Step firstStep = mockSuccessfulStep("step");
    when(collector.collectScenarioSteps(asList(mySteps), scenario1, new HashMap<String, String>())).thenReturn(asList(firstStep));
    boolean givenStory = false;
    givenStoryWithNoBeforeOrAfterSteps(story, givenStory, collector, mySteps);
    givenBeforeAndAfterScenarioSteps(ScenarioType.NORMAL, collector, mySteps);
    givenBeforeAndAfterScenarioSteps(ScenarioType.ANY, collector, mySteps);
    givenLifecycleSteps(collector, mySteps);
    // When
    StoryRunner runner = new StoryRunner();
    runner.run(configuration, asList(mySteps), story);
    // Then
    InOrder inOrder = inOrder(reporter, failureStrategy);
    inOrder.verify(reporter).successful(stepNameFor(Stage.BEFORE, ScenarioType.NORMAL));
    inOrder.verify(reporter).successful(stepNameFor(Stage.BEFORE, ScenarioType.ANY));
    inOrder.verify(reporter).successful(lifecycleStepNameFor(Stage.BEFORE));
    inOrder.verify(reporter).successful("step");
    inOrder.verify(reporter).successful(lifecycleStepNameFor(Stage.AFTER));
    inOrder.verify(reporter).successful(stepNameFor(Stage.AFTER, ScenarioType.ANY));
    inOrder.verify(reporter).successful(stepNameFor(Stage.AFTER, ScenarioType.NORMAL));
}
Also used : StoryReporter(org.jbehave.core.reporters.StoryReporter) ConcurrentStoryReporter(org.jbehave.core.reporters.ConcurrentStoryReporter) InOrder(org.mockito.InOrder) Configuration(org.jbehave.core.configuration.Configuration) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) AbstractStep(org.jbehave.core.steps.StepCreator.AbstractStep) Test(org.junit.Test)

Example 9 with Configuration

use of org.jbehave.core.configuration.Configuration in project jbehave-core by jbehave.

the class StoryRunnerBehaviour method shouldNotRunStoriesNotAllowedByFilterOnStoryElement.

@Test
public void shouldNotRunStoriesNotAllowedByFilterOnStoryElement() throws Throwable {
    // Given
    StoryReporter reporter = mock(ConcurrentStoryReporter.class);
    StepCollector collector = mock(StepCollector.class);
    FailureStrategy strategy = mock(FailureStrategy.class);
    CandidateSteps mySteps = new Steps();
    when(collector.collectScenarioSteps(eq(asList(mySteps)), (Scenario) anyObject(), eq(parameters))).thenReturn(Arrays.<Step>asList());
    Story story = new Story("excluded_path", Description.EMPTY, Meta.EMPTY, Narrative.EMPTY, asList(new Scenario()));
    boolean givenStory = false;
    givenStoryWithNoBeforeOrAfterSteps(story, givenStory, collector, mySteps);
    String filterAsString = "-story_path excluded_path";
    MetaFilter filter = new MetaFilter(filterAsString);
    // When
    StoryRunner runner = new StoryRunner();
    Configuration configuration = configurationWith(reporter, collector, strategy);
    configuration.storyControls().useStoryMetaPrefix("story_");
    runner.run(configuration, asList(mySteps), story, filter);
    // Then
    verify(reporter).beforeStory(story, givenStory);
    verify(reporter).storyNotAllowed(story, filterAsString);
    verify(reporter).afterStory(givenStory);
}
Also used : StoryReporter(org.jbehave.core.reporters.StoryReporter) ConcurrentStoryReporter(org.jbehave.core.reporters.ConcurrentStoryReporter) Configuration(org.jbehave.core.configuration.Configuration) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) Test(org.junit.Test)

Example 10 with Configuration

use of org.jbehave.core.configuration.Configuration in project jbehave-core by jbehave.

the class StoryRunnerBehaviour method shouldAllowToSkipBeforeAndAfterScenarioStepsIfGivenStory.

@Test
public void shouldAllowToSkipBeforeAndAfterScenarioStepsIfGivenStory() throws Throwable {
    // Given
    Scenario scenario1 = new Scenario("scenario 1", asList("successfulStep"));
    GivenStories givenStories = new GivenStories("/path/to/given/story1");
    Scenario scenario2 = new Scenario("scenario 2", Meta.EMPTY, givenStories, ExamplesTable.EMPTY, asList("anotherSuccessfulStep"));
    Story story1 = new Story(new Description("story 1"), Narrative.EMPTY, asList(scenario1));
    Story story2 = new Story(new Description("story 2"), Narrative.EMPTY, asList(scenario2));
    Step step = mock(Step.class);
    StepResult result = mock(StepResult.class);
    when(step.perform(null)).thenReturn(result);
    StoryParser storyParser = mock(StoryParser.class);
    StoryLoader storyLoader = mock(StoryLoader.class);
    StoryReporter reporter = mock(ConcurrentStoryReporter.class);
    StepCollector collector = mock(StepCollector.class);
    CandidateSteps mySteps = new Steps();
    Step successfulStep = mockSuccessfulStep("successfulStep");
    Step anotherSuccessfulStep = mockSuccessfulStep("anotherSuccessfulStep");
    givenStoryWithNoBeforeOrAfterSteps(story1, false, collector, mySteps);
    when(collector.collectScenarioSteps(asList(mySteps), scenario1, parameters)).thenReturn(asList(successfulStep));
    givenStoryWithNoBeforeOrAfterSteps(story2, true, collector, mySteps);
    when(collector.collectScenarioSteps(asList(mySteps), scenario2, parameters)).thenReturn(asList(anotherSuccessfulStep));
    when(storyLoader.loadStoryAsText("/path/to/given/story1")).thenReturn("storyContent");
    when(storyParser.parseStory("storyContent", "/path/to/given/story1")).thenReturn(story1);
    FailureStrategy failureStrategy = mock(FailureStrategy.class);
    Step beforeStep = mockSuccessfulStep("SuccessfulBeforeScenarioStep");
    Step afterStep = mockSuccessfulStep("SuccessfulAfterScenarioStep");
    when(collector.collectBeforeOrAfterScenarioSteps(eq(asList(mySteps)), Matchers.<Meta>any(), eq(Stage.BEFORE), eq(ScenarioType.NORMAL))).thenReturn(asList(beforeStep));
    when(collector.collectBeforeOrAfterScenarioSteps(eq(asList(mySteps)), Matchers.<Meta>any(), eq(Stage.AFTER), eq(ScenarioType.NORMAL))).thenReturn(asList(afterStep));
    // When
    StoryRunner runner = new StoryRunner();
    Configuration configuration = configurationWith(storyParser, storyLoader, reporter, collector, failureStrategy);
    configuration.storyControls().doSkipBeforeAndAfterScenarioStepsIfGivenStory(true);
    runner.run(configuration, asList(mySteps), story2);
    // Then
    verify(collector).collectScenarioSteps(asList(mySteps), scenario1, parameters);
    verify(collector).collectScenarioSteps(asList(mySteps), scenario2, parameters);
    InOrder inOrder = inOrder(beforeStep, successfulStep, anotherSuccessfulStep, afterStep);
    inOrder.verify(beforeStep).perform(null);
    inOrder.verify(successfulStep).perform(null);
    inOrder.verify(anotherSuccessfulStep).perform(null);
    inOrder.verify(afterStep).perform(null);
}
Also used : StoryReporter(org.jbehave.core.reporters.StoryReporter) ConcurrentStoryReporter(org.jbehave.core.reporters.ConcurrentStoryReporter) StoryLoader(org.jbehave.core.io.StoryLoader) InOrder(org.mockito.InOrder) Configuration(org.jbehave.core.configuration.Configuration) MostUsefulConfiguration(org.jbehave.core.configuration.MostUsefulConfiguration) AbstractStep(org.jbehave.core.steps.StepCreator.AbstractStep) RegexStoryParser(org.jbehave.core.parsers.RegexStoryParser) StoryParser(org.jbehave.core.parsers.StoryParser) AbstractStepResult(org.jbehave.core.steps.AbstractStepResult) Test(org.junit.Test)

Aggregations

Configuration (org.jbehave.core.configuration.Configuration)64 MostUsefulConfiguration (org.jbehave.core.configuration.MostUsefulConfiguration)61 Test (org.junit.Test)55 AnnotationBuilder (org.jbehave.core.configuration.AnnotationBuilder)18 StoryReporter (org.jbehave.core.reporters.StoryReporter)15 ConcurrentStoryReporter (org.jbehave.core.reporters.ConcurrentStoryReporter)13 StoryPathResolver (org.jbehave.core.io.StoryPathResolver)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 OutputStream (java.io.OutputStream)10 PrintStream (java.io.PrintStream)10 Matchers.containsString (org.hamcrest.Matchers.containsString)10 InjectableEmbedder (org.jbehave.core.InjectableEmbedder)10 UsingEmbedder (org.jbehave.core.annotations.UsingEmbedder)10 AbstractStep (org.jbehave.core.steps.StepCreator.AbstractStep)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 JUnitStory (org.jbehave.core.junit.JUnitStory)8 Story (org.jbehave.core.model.Story)8 SimpleDateFormat (java.text.SimpleDateFormat)7 RunContext (org.jbehave.core.embedder.PerformableTree.RunContext)7