Skip to content

Commit 11ac9ff

Browse files
committed
Make it possible to inject a glue when creating a Runtime
To aid testing, make it possible to inject a glue when creating a Runtime. Also add a test specify that a CucumberException is thrown if no backends are found (or an emtpy collection of backends is injected).
1 parent 5cd20af commit 11ac9ff

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

core/src/main/java/cucumber/runtime/Runtime.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,24 @@ public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, RuntimeOp
5151
}
5252

5353
public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collection<? extends Backend> backends, RuntimeOptions runtimeOptions) {
54-
this.resourceLoader = resourceLoader;
55-
this.classLoader = classLoader;
54+
this(resourceLoader, classLoader, backends, runtimeOptions, null);
55+
}
56+
57+
public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collection<? extends Backend> backends,
58+
RuntimeOptions runtimeOptions, RuntimeGlue optionalGlue) {
5659
if (backends.isEmpty()) {
5760
throw new CucumberException("No backends were found. Please make sure you have a backend module on your CLASSPATH.");
5861
}
62+
this.resourceLoader = resourceLoader;
63+
this.classLoader = classLoader;
5964
this.backends = backends;
60-
glue = new RuntimeGlue(undefinedStepsTracker, new LocalizedXStreams(classLoader));
65+
this.runtimeOptions = runtimeOptions;
66+
this.glue = optionalGlue != null ? optionalGlue : new RuntimeGlue(undefinedStepsTracker, new LocalizedXStreams(classLoader));
6167

6268
for (Backend backend : backends) {
6369
backend.loadGlue(glue, runtimeOptions.getGlue());
6470
backend.setUnreportedStepExecutor(this);
6571
}
66-
this.runtimeOptions = runtimeOptions;
6772
}
6873

6974
private static Collection<? extends Backend> loadBackends(ResourceLoader resourceLoader, ClassLoader classLoader) {

core/src/test/java/cucumber/runtime/RuntimeTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313

1414
import java.util.Arrays;
1515
import java.util.Collection;
16+
import java.util.Collections;
1617
import java.util.List;
1718
import java.util.Properties;
1819

1920
import static cucumber.runtime.TestHelper.feature;
2021
import static java.util.Arrays.asList;
2122
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.fail;
2224
import static org.mockito.Mockito.mock;
2325

2426
public class RuntimeTest {
@@ -163,6 +165,18 @@ public void strict_with_errors() {
163165
assertEquals(0x1, runtime.exitStatus());
164166
}
165167

168+
@Test
169+
public void should_throw_cucumer_exception_if_no_backends_are_found() throws Exception {
170+
try {
171+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
172+
new Runtime(new ClasspathResourceLoader(classLoader), classLoader, Collections.<Backend>emptyList(),
173+
new RuntimeOptions(new Properties()));
174+
fail("A CucumberException should have been thrown");
175+
} catch (CucumberException e) {
176+
assertEquals("No backends were found. Please make sure you have a backend module on your CLASSPATH.", e.getMessage());
177+
}
178+
}
179+
166180
private Runtime createStrictRuntime() {
167181
return createRuntime("-g", "anything", "--strict");
168182
}

0 commit comments

Comments
 (0)