Skip to content

Commit 3c75009

Browse files
committed
Merge pull request #763 from jakecollins/fix-guice-docs
Update links and fix formatting in Cucumber Guice docs.
2 parents 52987aa + bb28bb9 commit 3c75009

File tree

4 files changed

+75
-62
lines changed

4 files changed

+75
-62
lines changed

guice/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Using Cucumber Guice
22

3-
The [package documentation](src/main/java/cucumber/api/guice/package.html) contains detailed instructions for using
4-
Cucumber Guice. In particular be sure to read the migration section if upgrading from earlier versions of Cucumber
5-
Guice.
3+
The package documentation contains detailed instructions for using Cucumber Guice. In particular be sure to read the
4+
migration section if upgrading from earlier versions of Cucumber Guice.
5+
6+
[Read package documentation online at cukes.info]
7+
(http://cukes.info/api/cucumber/jvm/javadoc/cucumber/api/guice/package-summary.html)
8+
9+
[Read package documentation offline (raw html)](src/main/java/cucumber/api/guice/package.html)

guice/src/main/java/cucumber/api/guice/CucumberModules.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/**
77
* Provides a convenient <code>com.google.inject.Module</code> instance that contains bindings for
8-
* code>cucumber.runtime.java.guice.ScenarioScoped</code> annotation and for
8+
* <code>cucumber.runtime.java.guice.ScenarioScoped</code> annotation and for
99
* <code>cucumber.runtime.java.guice.ScenarioScope</code>.
1010
*/
1111
public class CucumberModules {

guice/src/main/java/cucumber/api/guice/README.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
/**
5-
* Please see documentation in package.html
5+
* Please see package documentation in package.html
66
*/
77
public class README {
88
}

guice/src/main/java/cucumber/api/guice/package.html

+66-57
Original file line numberDiff line numberDiff line change
@@ -3,116 +3,127 @@
33
This module allows you to use Google Guice dependency injection in your Cucumber tests. Guice comes as standard with
44
singleton scope and 'no scope'. This module adds Cucumber scenario scope to the scopes available for use in your
55
test code. The rest of this documentation assumes you have at least a basic understanding of Guice. Please refer to
6-
the Google documentation if necessary, see https://code.google.com/p/google-guice/wiki/Motivation?tm=6
7-
8-
<b>About scopes, injectors and migration from earlier versions</b>
9-
6+
the Guice wiki if necessary, see <a href="https://github.com/google/guice/wiki/Motivation"
7+
target="_parent">
8+
https://github.com/google/guice/wiki/Motivation</a>
9+
</p>
10+
<h3>About scopes, injectors and migration from earlier versions</h3>
11+
<p>
1012
It's important to realise the differences in how this module functions when compared with earlier versions. The
1113
changes are as follows.
12-
13-
<= 1.1.7 A Guice injector is created at the start of each test scenario and is destroyed at the end of each test
14-
scenario. There is no scenario scope, just singleton and 'no scope'.
15-
16-
> 1.1.7 A Guice injector is created once before any tests are run and is destroyed after the last test has run.
17-
Before each test scenario a new scenario scope is created. At the end of the test scenario the scenario
18-
scope is destroyed. Singleton scope exists throughout all test scenarios.
19-
20-
Users wishing to migrate to this version should replace <code>@Singleton</code> annotations with
21-
<code>@ScenarioScope</code> annotations. Guice modules should also have their singleton bindings updated. All
22-
bindings in <code>Scopes.SINGLETON</code> should be replaced with bindings in <code>CucumberScopes.SCENARIO</code>.
23-
24-
<b>Using the module</b>
25-
14+
</p>
15+
<h4>Version 1.1.7 and earlier</h4>
16+
<p>
17+
A Guice injector is created at the start of each test scenario and is destroyed at the end of each test scenario.
18+
There is no scenario scope, just singleton and 'no scope'.
19+
</p>
20+
<h4>Version 1.1.8 onwards</h4>
21+
<p>
22+
A Guice injector is created once before any tests are run and is destroyed after the last test has run. Before each
23+
test scenario a new scenario scope is created. At the end of the test scenario the scenario scope is destroyed.
24+
Singleton scope exists throughout all test scenarios.
25+
</p>
26+
<h4>Migrating to version 1.1.8 or later</h4>
27+
<p>
28+
Users wishing to migrate should replace <code>@Singleton</code> annotations with <code>@ScenarioScope</code>
29+
annotations. Guice modules should also have their singleton bindings updated. All bindings in
30+
<code>Scopes.SINGLETON</code> should be replaced with bindings in <code>CucumberScopes.SCENARIO</code>.
31+
</p>
32+
<h3>Using the module</h3>
33+
<p>
2634
By including the <code>cucumber-guice</code> jar on your <code>CLASSPATH</code> your Step Definitions will be
2735
instantiated by Guice. There are two main modes of using the module: with scope annotations and with module
2836
bindings. The two modes can also be mixed. When mixing modes it is important to realise that binding a class in a
2937
scope in a module takes precedence if the same class is also bound using a scope annotation.
30-
31-
<b>Scoping your step definitions</b>
32-
38+
</p>
39+
<h3>Scoping your step definitions</h3>
40+
<p>
3341
Usually you will want to bind your step definition classes in either scenario scope or in singleton scope. It is not
3442
recommended to leave your step definition classes with no scope as it means that Cucumber will instantiate a new
3543
instance of the class for each step within a scenario that uses that step definition.
36-
37-
<b>Scenario scope</b>
38-
44+
</p>
45+
<h3>Scenario scope</h3>
46+
<p>
3947
Cucumber will create exactly one instance of a class bound in scenario scope for each scenario in which it is used.
4048
You should use scenario scope when you want to store state during a scenario but do not want the state to interfere
4149
with subsequent scenarios.
42-
43-
<b>Singleton scope</b>
44-
50+
</p>
51+
<p>
52+
<h3>Singleton scope</h3>
53+
<p>
4554
Cucumber will create just one instance of a class bound in singleton scope that will last for the lifetime of all
4655
test scenarios in the test run. You should use singleton scope if your classes are stateless. You can also use
4756
singleton scope when your classes contain state but with caution. You should be absolutely sure that a state change
4857
in one scenario could not possibly influence the success or failure of a subsequent scenario. As an example of when
4958
you might use a singleton, imagine you have an http client that is expensive to create. By holding a reference to
5059
the client in a class bound in singleton scope you can reuse the client in multiple scenarios.
51-
52-
<b>Using scope annotations</b>
53-
60+
</p>
61+
<h3>Using scope annotations</h3>
62+
<p>
5463
This is the easy route if you're new to Guice. To bind a class in scenario scope add the
5564
<code>cucumber.runtime.java.guice.ScenarioScoped</code> annotation to the class definition. The class should have
5665
a no-args constructor or one constructor that is annotated with <code>javax.inject.Inject</code>. For example:
57-
58-
<code>
66+
</p>
67+
<pre>
5968
import cucumber.runtime.java.guice.ScenarioScoped;
6069
import javax.inject.Inject;
6170

62-
@ScenarioScoped
71+
{@literal @}ScenarioScoped
6372
public class ScenarioScopedSteps {
6473

6574
private final Object someInjectedDependency;
6675

67-
@Inject
76+
{@literal @}Inject
6877
public ScenarioScopedSteps(Object someInjectedDependency) {
6978
this.someInjectedDependency = someInjectedDependency;
7079
}
7180

7281
...
7382
}
74-
</code>
75-
83+
</pre>
84+
<p>
7685
To bind a class in singleton scope add the <code>javax.inject.Singleton</code> annotation to the class definition.
7786
One strategy for using stateless step definitions is to use providers to share stateful scenario scoped instances
7887
between stateless singleton step definition instances. For example:
79-
80-
<code>
88+
</p>
89+
<pre>
8190
import javax.inject.Inject;
8291
import javax.inject.Singleton;
8392

84-
@Singleton
93+
{@literal @}Singleton
8594
public class MyStatelessSteps {
8695

8796
private final Provider&lt;MyStatefulObject&gt; providerMyStatefulObject;
8897

89-
@Inject
98+
{@literal @}Inject
9099
public MyStatelessSteps(Provider&lt;MyStatefulObject&gt; providerMyStatefulObject) {
91100
this.providerMyStatefulObject = providerMyStatefulObject;
92101
}
93102

94-
@Given("^I have (\\d+) cukes in my belly$")
103+
{@literal @}Given("^I have (\\d+) cukes in my belly$")
95104
public void I_have_cukes_in_my_belly(int n) {
96105
providerMyStatefulObject.get().iHaveCukesInMyBelly(n);
97106
}
98107

99108
...
100109
}
101-
</code>
102-
103-
See https://code.google.com/p/google-guice/wiki/InjectingProviders "Providers for Mixing Scopes"
104-
105-
<b>Using module bindings</b>
106-
110+
</pre>
111+
<p>
112+
There is an alternative explanation of using
113+
<a href="https://github.com/google/guice/wiki/InjectingProviders#providers-for-mixing-scopes" target="_parent">
114+
providers for mixing scopes</a> on the Guice wiki.
115+
</p>
116+
<h3>Using module bindings</h3>
117+
<p>
107118
As an alternative to using annotations you may prefer to declare Guice bindings in a class that implements
108119
<code>com.google.inject.Module</code>. To do this you should create a class that implements
109120
<code>cucumber.runtime.java.guice.InjectorSource</code>. This gives you complete control over how you obtain a
110121
Guice injector and it's Guice modules. The injector must provide a binding for
111122
<code>cucumber.runtime.java.guice.ScenarioScope</code>. It should also provide a binding for the
112123
<code>cucumber.runtime.java.guice.ScenarioScoped</code> annotation if your classes are using the annotation. The
113124
easiest way to do this it to use <code>CucumberModules.SCENARIO</code>. For example:
114-
115-
<code>
125+
</p>
126+
<pre>
116127
import com.google.inject.Guice;
117128
import com.google.inject.Injector;
118129
import com.google.inject.Stage;
@@ -121,21 +132,19 @@
121132

122133
public class YourInjectorSource implements InjectorSource {
123134

124-
@Override
135+
{@literal @}Override
125136
public Injector getInjector() {
126137
return Guice.createInjector(Stage.PRODUCTION, CucumberModules.SCENARIO, new YourModule());
127138
}
128139
}
129-
</code>
130-
140+
</pre>
141+
<p>
131142
Cucumber needs to know where to find the <code>cucumber.runtime.java.guice.InjectorSource</code> that it will use.
132143
You should create a properties file called <code>cucumber-guice.properties</code> and place it in the root of the
133144
classpath. The file should contain a single property key called <code>guice.injector-source</code> with a value
134145
equal to the fully qualified name of the <code>cucumber.runtime.java.guice.InjectorSource</code>. For example:
135-
136-
<code>
137-
guice.injector-source=com.company.YourInjectorSource
138-
</code>
139-
140146
</p>
147+
<pre>
148+
guice.injector-source=com.company.YourInjectorSource
149+
</pre>
141150
</body>

0 commit comments

Comments
 (0)