Skip to content

Commit 9882451

Browse files
committed
Merge branch 'master' of git://github.com/Luxor/cucumber-jvm into Luxor-master
2 parents f548faa + 0356a97 commit 9882451

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

groovy/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
<artifactId>cobertura</artifactId>
5353
<scope>test</scope>
5454
</dependency>
55+
<dependency>
56+
<groupId>org.mockito</groupId>
57+
<artifactId>mockito-all</artifactId>
58+
<scope>test</scope>
59+
</dependency>
5560
</dependencies>
5661

5762
<build>

groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GroovyBackend implements Backend {
3737
private final ClasspathResourceLoader classpathResourceLoader;
3838

3939
private Closure worldClosure;
40-
private Object groovyWorld;
40+
private Object world;
4141
private Glue glue;
4242

4343
private static GroovyShell createShell() {
@@ -97,6 +97,7 @@ public void setUnreportedStepExecutor(UnreportedStepExecutor executor) {
9797

9898
@Override
9999
public void buildWorld() {
100+
world = worldClosure == null ? new Object() : worldClosure.call();
100101
}
101102

102103
private Script parse(Resource resource) {
@@ -113,7 +114,7 @@ private boolean isScript(Script script) {
113114

114115
@Override
115116
public void disposeWorld() {
116-
this.groovyWorld = null;
117+
this.world = null;
117118
}
118119

119120
@Override
@@ -126,6 +127,7 @@ public void addStepDefinition(Pattern regexp, int timeoutMillis, Closure body) {
126127
}
127128

128129
public void registerWorld(Closure closure) {
130+
if (worldClosure != null) throw new CucumberException("World is already set");
129131
worldClosure = closure;
130132
}
131133

@@ -138,21 +140,14 @@ public void addAfterHook(TagExpression tagExpression, int timeoutMillis, Closure
138140
}
139141

140142
public void invoke(Closure body, Object[] args) throws Throwable {
141-
body.setDelegate(getGroovyWorld());
143+
body.setDelegate(world);
142144
try {
143145
body.call(args);
144146
} catch (InvokerInvocationException e) {
145147
throw e.getCause();
146148
}
147149
}
148150

149-
private Object getGroovyWorld() {
150-
if (groovyWorld == null) {
151-
groovyWorld = worldClosure == null ? new Object() : worldClosure.call();
152-
}
153-
return groovyWorld;
154-
}
155-
156151
private static StackTraceElement currentLocation() {
157152
Throwable t = new Throwable();
158153
StackTraceElement[] stackTraceElements = t.getStackTrace();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cucumber.runtime.groovy;
2+
3+
import cucumber.runtime.CucumberException;
4+
import cucumber.runtime.io.ResourceLoader;
5+
import groovy.lang.Closure;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.mockito.Mock;
10+
import org.mockito.runners.MockitoJUnitRunner;
11+
12+
import static org.mockito.Mockito.verify;
13+
14+
15+
@RunWith(MockitoJUnitRunner.class)
16+
public class GroovyBackendTest {
17+
@Mock
18+
ResourceLoader resourceLoader;
19+
@Mock
20+
Closure closure;
21+
22+
GroovyBackend backend;
23+
24+
@Before
25+
public void setUp() throws Exception {
26+
backend = new GroovyBackend(resourceLoader);
27+
}
28+
29+
@Test
30+
public void builds_world_by_calling_closure() {
31+
backend.registerWorld(closure);
32+
backend.buildWorld();
33+
34+
verify(closure).call();
35+
}
36+
37+
@Test
38+
public void builds_default_wold_if_wold_closer_does_not_set() {
39+
backend.buildWorld();
40+
}
41+
42+
@Test(expected = CucumberException.class)
43+
public void raises_exception_for_two_wolds() {
44+
backend.registerWorld(closure);
45+
backend.registerWorld(closure);
46+
}
47+
}

0 commit comments

Comments
 (0)