Search in sources :

Example 11 with Embeddable

use of org.jbehave.core.Embeddable in project jbehave-core by jbehave.

the class EmbedderBehaviour method shouldRunStoriesAsEmbeddablesInBatchIfBatchFlagIsSet.

@Test
public void shouldRunStoriesAsEmbeddablesInBatchIfBatchFlagIsSet() throws Throwable {
    // Given
    PerformableTree performableTree = mock(PerformableTree.class);
    EmbedderControls embedderControls = new EmbedderControls().doBatch(true);
    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
    Embedder embedder = embedderWith(performableTree, embedderControls, monitor);
    embedder.useClassLoader(classLoader);
    embedder.runAsEmbeddables(classNames);
    // Then
    for (Embeddable story : embeddables) {
        String name = story.getClass().getName();
        assertThat(out.toString(), containsString("Running embeddable " + name));
    }
}
Also used : PrintStream(java.io.PrintStream) 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) Test(org.junit.Test)

Example 12 with Embeddable

use of org.jbehave.core.Embeddable 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 13 with Embeddable

use of org.jbehave.core.Embeddable in project jbehave-core by jbehave.

the class Embedder method runStoriesWithAnnotatedEmbedderRunner.

public void runStoriesWithAnnotatedEmbedderRunner(List<String> classNames) {
    EmbedderClassLoader classLoader = classLoader();
    for (String className : classNames) {
        embedderMonitor.runningWithAnnotatedEmbedderRunner(className);
        AnnotatedEmbedderRunner runner = AnnotatedEmbedderUtils.annotatedEmbedderRunner(className, classLoader);
        try {
            Object annotatedInstance = runner.createTest();
            if (annotatedInstance instanceof Embeddable) {
                ((Embeddable) annotatedInstance).run();
            } else {
                embedderMonitor.annotatedInstanceNotOfType(annotatedInstance, Embeddable.class);
            }
        } catch (Throwable e) {
            throw new AnnotatedEmbedderRunFailed(runner, e);
        }
    }
}
Also used : AnnotatedEmbedderRunner(org.jbehave.core.junit.AnnotatedEmbedderRunner) Embeddable(org.jbehave.core.Embeddable)

Example 14 with Embeddable

use of org.jbehave.core.Embeddable in project jbehave-core by jbehave.

the class AnnotationBuilder method injectEmbedder.

protected Object injectEmbedder(Embedder embedder, Class<?> annotatedClass) {
    try {
        Object instance = annotatedClass.newInstance();
        if (instance instanceof Embeddable) {
            Embeddable embeddable = (Embeddable) instance;
            embeddable.useEmbedder(embedder);
        }
        if (instance instanceof ConfigurableEmbedder) {
            ConfigurableEmbedder configurableEmbedder = (ConfigurableEmbedder) instance;
            configurableEmbedder.useConfiguration(embedder.configuration());
            configurableEmbedder.addSteps(embedder.candidateSteps());
            configurableEmbedder.useStepsFactory(embedder.stepsFactory());
        }
        return instance;
    } catch (Exception e) {
        annotationMonitor.elementCreationFailed(annotatedClass, e);
        throw new InstantiationFailed(annotatedClass, e);
    }
}
Also used : ConfigurableEmbedder(org.jbehave.core.ConfigurableEmbedder) IOException(java.io.IOException) Embeddable(org.jbehave.core.Embeddable)

Aggregations

Embeddable (org.jbehave.core.Embeddable)14 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 Test (org.junit.Test)10 ConfigurableEmbedder (org.jbehave.core.ConfigurableEmbedder)2 Configuration (org.jbehave.core.configuration.Configuration)2 MostUsefulConfiguration (org.jbehave.core.configuration.MostUsefulConfiguration)2 UnderscoredCamelCaseResolver (org.jbehave.core.io.UnderscoredCamelCaseResolver)2 CandidateSteps (org.jbehave.core.steps.CandidateSteps)2 IOException (java.io.IOException)1 BatchFailures (org.jbehave.core.failures.BatchFailures)1 AnnotatedEmbedderRunner (org.jbehave.core.junit.AnnotatedEmbedderRunner)1