diff --git a/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java b/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java index 25136af2f8..a1cd13dec8 100644 --- a/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java +++ b/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java @@ -2,6 +2,7 @@ import io.cucumber.core.backend.CucumberBackendException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.Scope; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.ConfigurableApplicationContext; @@ -60,7 +61,13 @@ private void notifyTestContextManagerAboutBeforeTestMethod() { final void registerGlueCodeScope(ConfigurableApplicationContext context) { while (context != null) { - context.getBeanFactory().registerScope(SCOPE_CUCUMBER_GLUE, new CucumberScenarioScope()); + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); + // Scenario scope may have already been registered by another + // thread. + Scope registeredScope = beanFactory.getRegisteredScope(SCOPE_CUCUMBER_GLUE); + if (registeredScope == null) { + beanFactory.registerScope(SCOPE_CUCUMBER_GLUE, new CucumberScenarioScope()); + } context = (ConfigurableApplicationContext) context.getParent(); } }