Skip to content

Commit 8f11a19

Browse files
committed
[Guice] Fix NPE in Guice when configured incorrectly
Fixes: #2716
1 parent 789f6fc commit 8f11a19

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Fixed
2020
- [Pico] Improve performance ([#2724](https://github.com/cucumber/cucumber-jvm/issues/2724) Julien Kronegg)
2121
- [JUnit 4] Fix swallowed exception ([#2714](https://github.com/cucumber/cucumber-jvm/issues/2714) M.P. Korstanje)
22+
- [Guice] Fix NPE in Guice when configured incorrectly ([#2716](https://github.com/cucumber/cucumber-jvm/issues/2716) M.P. Korstanje)
2223

2324
## [7.11.2] - 2023-03-23
2425
### Fixed

cucumber-guice/src/main/java/io/cucumber/guice/GuiceFactory.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ public final class GuiceFactory implements ObjectFactory {
2424

2525
private final Collection<Class<?>> stepClasses = new HashSet<>();
2626
private final Class<?> injectorSourceFromProperty;
27-
private Class<?> withInjectorSource = null;
27+
private Class<?> withInjectorSource;
28+
private ScenarioScope scenarioScope;
2829

2930
public GuiceFactory() {
30-
injectorSourceFromProperty = loadInjectorSourceFromProperties(CucumberProperties.create());
31+
this.injectorSourceFromProperty = loadInjectorSourceFromProperties(CucumberProperties.create());
3132
}
3233

3334
@Override
@@ -72,11 +73,15 @@ public void start() {
7273
injector = new InjectorSourceFactory(withInjectorSource).create()
7374
.getInjector();
7475
}
75-
injector.getInstance(ScenarioScope.class).enterScope();
76+
scenarioScope = injector.getInstance(ScenarioScope.class);
77+
scenarioScope.enterScope();
7678
}
7779

7880
public void stop() {
79-
injector.getInstance(ScenarioScope.class).exitScope();
81+
if (scenarioScope != null) {
82+
scenarioScope.exitScope();
83+
scenarioScope = null;
84+
}
8085
}
8186

8287
public <T> T getInstance(Class<T> clazz) {

cucumber-guice/src/test/java/io/cucumber/guice/GuiceFactoryTest.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import static org.hamcrest.CoreMatchers.containsString;
2727
import static org.hamcrest.CoreMatchers.is;
2828
import static org.hamcrest.CoreMatchers.notNullValue;
29-
import static org.hamcrest.CoreMatchers.startsWith;
3029
import static org.hamcrest.MatcherAssert.assertThat;
31-
import static org.hamcrest.text.IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace;
3230
import static org.junit.jupiter.api.Assertions.assertThrows;
3331
import static org.junit.jupiter.api.Assertions.assertTrue;
3432

@@ -52,9 +50,8 @@ protected void configure() {
5250
void tearDown() {
5351
// If factory is left in start state it can cause cascading failures due
5452
// to scope being left open
55-
try {
53+
if (factory != null) {
5654
factory.stop();
57-
} catch (Exception ignored) {
5855
}
5956
}
6057

@@ -80,8 +77,7 @@ void factoryCanBeInstantiatedWithArgConstructor() {
8077
void factoryStartFailsIfScenarioScopeIsNotBound() {
8178
initFactory(Guice.createInjector());
8279

83-
Executable testMethod = () -> factory.start();
84-
ConfigurationException actualThrown = assertThrows(ConfigurationException.class, testMethod);
80+
ConfigurationException actualThrown = assertThrows(ConfigurationException.class, () -> factory.start());
8581
assertThat("Unexpected exception message", actualThrown.getMessage(),
8682
containsString("1) [Guice/MissingImplementation]: No implementation for ScenarioScope was bound."));
8783
}

0 commit comments

Comments
 (0)