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));
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
Aggregations