From c1beb21b4490bb54e692f46c8afc4fe3f9b8eff8 Mon Sep 17 00:00:00 2001
From: James Bennett
+ * Note that using this in combination with parallel execution results in
+ * undefined behaviour.
+ *
+ * @deprecated please use {@link #createScenarioModule()} instead
+ */
+ @Deprecated
+ public static final Module SCENARIO = createScenarioModule();
+
+ public static Module createScenarioModule() {
+ return new ScenarioModule(CucumberScopes.createScenarioScope());
+ }
+
+ public static Module createScenarioModule(ScenarioScope scenarioScope) {
+ return new ScenarioModule(scenarioScope);
+ }
}
diff --git a/guice/src/main/java/cucumber/api/guice/CucumberScopes.java b/guice/src/main/java/cucumber/api/guice/CucumberScopes.java
index b9dac00415..fafe7c6ba5 100644
--- a/guice/src/main/java/cucumber/api/guice/CucumberScopes.java
+++ b/guice/src/main/java/cucumber/api/guice/CucumberScopes.java
@@ -1,12 +1,39 @@
package cucumber.api.guice;
+import com.google.inject.Module;
import cucumber.runtime.java.guice.ScenarioScope;
+import cucumber.runtime.java.guice.ScenarioScoped;
import cucumber.runtime.java.guice.impl.SequentialScenarioScope;
/**
- * Provides a convenient
+ * Note that when binding objects to the scenario scope it is recommended to bind
+ * them to the {@link ScenarioScoped} annotation instead. E.g:
+ *
+ *
+ * Note that using this in combination with parallel execution results in
+ * undefined behaviour.
+ *
+ * @deprecated please use {@link #createScenarioScope()} instead
+ */
+ @Deprecated
+ public static final ScenarioScope SCENARIO = createScenarioScope();
+
+ /**
+ * Creates a new instance of a ScenarioScope.
+ *
+ * @return a new instance of a ScenarioScope.
+ */
+ public static ScenarioScope createScenarioScope() {
+ return new SequentialScenarioScope();
+ }
+
}
diff --git a/guice/src/main/java/cucumber/api/guice/package.html b/guice/src/main/java/cucumber/api/guice/package.html
index 317aaa8a9c..163e90fe92 100644
--- a/guice/src/main/java/cucumber/api/guice/package.html
+++ b/guice/src/main/java/cucumber/api/guice/package.html
@@ -121,7 +121,7 @@ com.google.inject.Module
instance that contains bindings for
- * cucumber.runtime.java.guice.ScenarioScoped
annotation and for
- * cucumber.runtime.java.guice.ScenarioScope
.
+ * Provides a convenient {@link Module} instance that contains bindings for
+ * {@link ScenarioScoped} annotation and for {@link ScenarioScope}.
*/
public class CucumberModules {
- public static final Module SCENARIO = new ScenarioModule(CucumberScopes.SCENARIO);
+ /**
+ * A convenient instance of {@link Module}. Should only be used
+ * in combination with {@link CucumberScopes#SCENARIO}.
+ * cucumber.runtime.java.guice.ScenarioScope
instance for use when declaring bindings
- * in implementations of com.google.inject.Module
.
+ * Creates an instance of {@link ScenarioScope} for use when declaring bindings
+ * in implementations of {@link Module}.
+ * bind(ScenarioScopedObject.class).in(ScenarioScoped.class);
*/
public class CucumberScopes {
- public static final ScenarioScope SCENARIO = new SequentialScenarioScope();
+ /**
+ * A convenient instance of {@link ScenarioScope}. Should only be used
+ * in combination with {@link CucumberModules#SCENARIO}.
+ * Using module bindings
Guice injector and it's Guice modules. The injector must provide a binding for
cucumber.runtime.java.guice.ScenarioScope
. It should also provide a binding for the
cucumber.runtime.java.guice.ScenarioScoped
annotation if your classes are using the annotation. The
- easiest way to do this it to use CucumberModules.SCENARIO
. For example:
+ easiest way to do this it to use CucumberModules.createScenarioModule()
. For example:
import com.google.inject.Guice; @@ -134,7 +134,7 @@diff --git a/guice/src/test/java/cucumber/runtime/java/guice/impl/GuiceFactoryTest.java b/guice/src/test/java/cucumber/runtime/java/guice/impl/GuiceFactoryTest.java index 11ada40622..90718db497 100644 --- a/guice/src/test/java/cucumber/runtime/java/guice/impl/GuiceFactoryTest.java +++ b/guice/src/test/java/cucumber/runtime/java/guice/impl/GuiceFactoryTest.java @@ -8,7 +8,6 @@ import com.google.inject.Scopes; import com.google.inject.Stage; import cucumber.api.guice.CucumberModules; -import cucumber.api.guice.CucumberScopes; import cucumber.api.java.ObjectFactory; import cucumber.runtime.java.guice.ScenarioScoped; import org.junit.After; @@ -65,14 +64,14 @@ static class UnscopedClass {} @Test public void shouldGiveNewInstancesOfUnscopedClassWithinAScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule())); instancesFromSameScenario = getInstancesFromSameScenario(factory, UnscopedClass.class); assertThat(instancesFromSameScenario, elementsAreAllUnique()); } @Test public void shouldGiveNewInstanceOfUnscopedClassForEachScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule())); instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, UnscopedClass.class); assertThat(instancesFromDifferentScenarios, elementsAreAllUnique()); } @@ -81,14 +80,14 @@ public void shouldGiveNewInstanceOfUnscopedClassForEachScenario() { @Test public void shouldGiveTheSameInstanceOfAnnotatedScenarioScopedClassWithinAScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule())); instancesFromSameScenario = getInstancesFromSameScenario(factory, AnnotatedScenarioScopedClass.class); assertThat(instancesFromSameScenario, elementsAreAllEqual()); } @Test public void shouldGiveNewInstanceOfAnnotatedScenarioScopedClassForEachScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule())); instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, AnnotatedScenarioScopedClass.class); assertThat(instancesFromDifferentScenarios, elementsAreAllUnique()); } @@ -97,14 +96,14 @@ public void shouldGiveNewInstanceOfAnnotatedScenarioScopedClassForEachScenario() @Test public void shouldGiveTheSameInstanceOfAnnotatedSingletonClassWithinAScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule())); instancesFromSameScenario = getInstancesFromSameScenario(factory, AnnotatedSingletonClass.class); assertThat(instancesFromSameScenario, elementsAreAllEqual()); } @Test public void shouldGiveTheSameInstanceOfAnnotatedSingletonClassForEachScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule())); instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, AnnotatedSingletonClass.class); assertThat(instancesFromDifferentScenarios, elementsAreAllEqual()); } @@ -113,13 +112,13 @@ static class BoundScenarioScopedClass {} final AbstractModule boundScenarioScopedClassModule = new AbstractModule() { @Override protected void configure() { - bind(BoundScenarioScopedClass.class).in(CucumberScopes.SCENARIO); + bind(BoundScenarioScopedClass.class).in(ScenarioScoped.class); } }; @Test public void shouldGiveTheSameInstanceOfBoundScenarioScopedClassWithinAScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO, boundScenarioScopedClassModule)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule(), boundScenarioScopedClassModule)); instancesFromSameScenario = getInstancesFromSameScenario(factory, BoundScenarioScopedClass.class); assertThat(instancesFromSameScenario, elementsAreAllEqual()); } @@ -148,17 +147,17 @@ public void shouldGiveTheSameInstanceOfBoundSingletonClassWithinAScenario() { @Test public void shouldGiveTheSameInstanceOfBoundSingletonClassForEachScenario() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO, boundSingletonClassModule)); + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule(), boundSingletonClassModule)); instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, BoundSingletonClass.class); assertThat(instancesFromDifferentScenarios, elementsAreAllEqual()); } @Test public void shouldGiveNewInstanceOfAnnotatedSingletonClassForEachScenarioWhenOverridingModuleBindingIsScenarioScope() { - factory = new GuiceFactory(injector(CucumberModules.SCENARIO, new AbstractModule() { + factory = new GuiceFactory(injector(CucumberModules.createScenarioModule(), new AbstractModule() { @Override protected void configure() { - bind(AnnotatedSingletonClass.class).in(CucumberScopes.SCENARIO); + bind(AnnotatedSingletonClass.class).in(ScenarioScoped.class); } })); instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, AnnotatedSingletonClass.class); @@ -202,4 +201,4 @@ privateUsing module bindings
{@literal @}Override public Injector getInjector() { - return Guice.createInjector(Stage.PRODUCTION, CucumberModules.SCENARIO, new YourModule()); + return Guice.createInjector(Stage.PRODUCTION, CucumberModules.createScenarioModule(), new YourModule()); } }