Search in sources :

Example 11 with BatchFailures

use of org.jbehave.core.failures.BatchFailures in project jbehave-core by jbehave.

the class EmbedderBehaviour method shouldRunFailingStoriesAsPathsInBatchIfBatchFlagIsSet.

@SuppressWarnings("unchecked")
@Test
public void shouldRunFailingStoriesAsPathsInBatchIfBatchFlagIsSet() throws Throwable {
    // Given
    PerformableTree performableTree = mock(PerformableTree.class);
    EmbedderControls embedderControls = new EmbedderControls().doBatch(true).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);
    BatchFailures failures = new BatchFailures();
    for (String storyPath : storyPaths) {
        RuntimeException thrown = new RuntimeException(storyPath + " failed");
        failures.put(storyPath, thrown);
        doThrow(thrown).when(performableTree).perform(runContext, stories.get(storyPath));
    }
    // When
    embedder.runStoriesAsPaths(storyPaths);
    // Then
    String output = out.toString();
    for (String storyPath : storyPaths) {
        assertThat(output, containsString("Running story " + storyPath));
        assertThat(output, 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 12 with BatchFailures

use of org.jbehave.core.failures.BatchFailures in project jbehave-core by jbehave.

the class EmbedderMojoBehaviour method shouldCreateNewEmbedderWithMavenMonitor.

@Test
public void shouldCreateNewEmbedderWithMavenMonitor() {
    // Given
    Log log = mock(Log.class);
    AbstractEmbedderMojo mojo = new AbstractEmbedderMojo() {

        public void execute() throws MojoExecutionException, MojoFailureException {
        }
    };
    mojo.setLog(log);
    // When
    Embedder embedder = mojo.newEmbedder();
    // Then
    EmbedderMonitor embedderMonitor = embedder.embedderMonitor();
    assertThat(embedderMonitor.toString(), containsString("MavenEmbedderMonitor"));
    // and verify monitor calls are propagated to Mojo Log
    BatchFailures failures = new BatchFailures();
    embedderMonitor.batchFailed(failures);
    verify(log).warn("Failed to run batch " + failures);
    String name = "name";
    Throwable cause = new RuntimeException();
    embedderMonitor.embeddableFailed(name, cause);
    verify(log).warn("Failed to run embeddable " + name, cause);
    List<String> classNames = asList("name1", "name2");
    embedderMonitor.embeddablesSkipped(classNames);
    verify(log).info("Skipped embeddables " + classNames);
    embedderMonitor.runningEmbeddable(name);
    verify(log).info("Running embeddable " + name);
    List<String> storyPaths = asList("/path1", "/path2");
    embedderMonitor.storiesSkipped(storyPaths);
    verify(log).info("Skipped stories " + storyPaths);
    String path = "/path";
    embedderMonitor.storyFailed(path, cause);
    verify(log).warn("Failed to run story " + path, cause);
    embedderMonitor.runningStory(path);
    verify(log).info("Running story " + path);
    Object annotatedInstance = new Object();
    Class<?> type = Object.class;
    embedderMonitor.annotatedInstanceNotOfType(annotatedInstance, type);
    verify(log).warn("Annotated instance " + annotatedInstance + " not of type " + type);
    File outputDirectory = new File("/dir");
    List<String> formats = asList(Format.CONSOLE.name(), Format.HTML.name());
    Properties viewProperties = new Properties();
    embedderMonitor.generatingReportsView(outputDirectory, formats, viewProperties);
    verify(log).info("Generating reports view to '" + outputDirectory + "' using formats '" + formats + "'" + " and view properties '" + viewProperties + "'");
    embedderMonitor.reportsViewGenerationFailed(outputDirectory, formats, viewProperties, cause);
    verify(log).warn("Failed to generate reports view to '" + outputDirectory + "' using formats '" + formats + "'" + " and view properties '" + viewProperties + "'", cause);
    int stories = 2;
    int storiesNotAllowed = 1;
    int storiesPending = 1;
    int scenarios = 4;
    int scenariosFailed = 1;
    int scenariosNotAllowed = 0;
    int scenariosPending = 1;
    int stepsFailed = 1;
    embedderMonitor.reportsViewGenerated(new ReportsCount(stories, storiesNotAllowed, storiesPending, scenarios, scenariosFailed, scenariosNotAllowed, scenariosPending, stepsFailed));
    verify(log).info("Reports view generated with " + stories + " stories (of which " + storiesPending + " pending) containing " + scenarios + " scenarios (of which " + scenariosPending + " pending)");
    verify(log).info("Meta filters excluded " + storiesNotAllowed + " stories and  " + scenariosNotAllowed + " scenarios");
    embedderMonitor.reportsViewNotGenerated();
    verify(log).info("Reports view not generated");
}
Also used : EmbedderMonitor(org.jbehave.core.embedder.EmbedderMonitor) Log(org.apache.maven.plugin.logging.Log) Embedder(org.jbehave.core.embedder.Embedder) InjectableEmbedder(org.jbehave.core.InjectableEmbedder) Matchers.containsString(org.hamcrest.Matchers.containsString) Properties(java.util.Properties) BatchFailures(org.jbehave.core.failures.BatchFailures) ReportsCount(org.jbehave.core.reporters.ReportsCount) File(java.io.File) Test(org.junit.Test)

Example 13 with BatchFailures

use of org.jbehave.core.failures.BatchFailures in project jbehave-core by jbehave.

the class Embedder method runAsEmbeddables.

public void runAsEmbeddables(List<String> classNames) {
    EmbedderControls embedderControls = embedderControls();
    embedderMonitor.usingControls(embedderControls);
    if (embedderControls.skip()) {
        embedderMonitor.embeddablesSkipped(classNames);
        return;
    }
    BatchFailures failures = new BatchFailures(embedderControls.verboseFailures());
    for (Embeddable embeddable : embeddables(classNames, classLoader())) {
        String name = embeddable.getClass().getName();
        try {
            embedderMonitor.runningEmbeddable(name);
            embeddable.useEmbedder(this);
            embeddable.run();
        } catch (Throwable e) {
            if (embedderControls.batch()) {
                // collect and postpone decision to throw exception
                failures.put(name, e);
            } else {
                if (ignoreFailure(embedderControls)) {
                    embedderMonitor.embeddableFailed(name, e);
                } else {
                    throw new RunningEmbeddablesFailed(name, e);
                }
            }
        }
    }
    if (embedderControls.batch() && failures.size() > 0) {
        if (ignoreFailure(embedderControls)) {
            embedderMonitor.batchFailed(failures);
        } else {
            throw new RunningEmbeddablesFailed(failures);
        }
    }
}
Also used : BatchFailures(org.jbehave.core.failures.BatchFailures) Embeddable(org.jbehave.core.Embeddable)

Example 14 with BatchFailures

use of org.jbehave.core.failures.BatchFailures in project jbehave-core by jbehave.

the class Embedder method runStoriesAsPaths.

public void runStoriesAsPaths(List<String> storyPaths) {
    processSystemProperties();
    EmbedderControls embedderControls = embedderControls();
    embedderMonitor.usingControls(embedderControls);
    if (embedderControls.skip()) {
        embedderMonitor.storiesSkipped(storyPaths);
        return;
    }
    try {
        // set up run context
        StoryManager storyManager = storyManager();
        MetaFilter filter = metaFilter();
        BatchFailures failures = new BatchFailures(embedderControls.verboseFailures());
        // run stories
        storyManager.runStoriesAsPaths(storyPaths, filter, failures);
        // handle any failures
        handleFailures(failures);
    } finally {
        // (if configured to do so)
        try {
            if (embedderControls.generateViewAfterStories()) {
                generateReportsView();
            }
        } finally {
            // shutdown regardless of failures in reports view
            shutdownExecutorService();
        }
    }
}
Also used : BatchFailures(org.jbehave.core.failures.BatchFailures)

Aggregations

BatchFailures (org.jbehave.core.failures.BatchFailures)14 Test (org.junit.Test)11 Matchers.containsString (org.hamcrest.Matchers.containsString)10 InjectableEmbedder (org.jbehave.core.InjectableEmbedder)9 OutputStream (java.io.OutputStream)8 PrintStream (java.io.PrintStream)8 RunContext (org.jbehave.core.embedder.PerformableTree.RunContext)8 Story (org.jbehave.core.model.Story)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 UsingEmbedder (org.jbehave.core.annotations.UsingEmbedder)7 Configuration (org.jbehave.core.configuration.Configuration)7 MostUsefulConfiguration (org.jbehave.core.configuration.MostUsefulConfiguration)7 StoryPathResolver (org.jbehave.core.io.StoryPathResolver)7 JUnitStory (org.jbehave.core.junit.JUnitStory)7 InjectableStepsFactory (org.jbehave.core.steps.InjectableStepsFactory)7 File (java.io.File)4 Properties (java.util.Properties)4 ReportsCount (org.jbehave.core.reporters.ReportsCount)4