Cucumber 没有为作用域名称注册的作用域 'cucumber-glue'

Cucumber No Scope registered for scope name 'cucumber-glue'

我有 No Scope registered for scope name 'cucumber-glue' 错误,而 运行 Cucumber (6.1.1) BDD 测试 Spring

我的配置文件

@ContextConfiguration(locations = {"classpath:/application-context-bdd.xml"})
@CucumberContextConfiguration
public class Configuration {

}

我的测试文件

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {
        "json:target/cucumber-json-report.json" },
        glue = {"xx/java/bdd/configuration", "xx/java/bdd/stepdefs", "xx/java/bdd/steps"},
        features = "classpath:features",
        publish = false
)
public class TestBDD {
}

我的应用程序上下文文件,其中声明了我的 cucumber-glue bean。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   
    <bean name="todoRepository" class="xx.java.core.domain.mock.TodoRepositoryMock" scope="cucumber-glue"/>

    <bean name="todoService" class="xx.java.core.domain.todolist.service.impl.TodoListServiceImpl">
        <constructor-arg ref="todoRepository"/>
    </bean>

    <bean name="todoStep" class="xx.java.bdd.steps.TodoStep">
        <constructor-arg ref="todoService"/>
    </bean>

    <bean class="xxx.java.bdd.stepdefs.TodoStepDefinition"> <!-- Not needed since @M.P. Korstanje reply -->
        <constructor-arg ref="todoStep"/>
    </bean>
</beans>

有什么想法吗??

正确的范围是cucumber-glue,但它仅在场景处于活动状态时存在。通过在 xml 中定义一个 bean,它将在应用程序启动时创建,在任何场景开始之前。

虽然您不需要将 TodoStepDefinition 定义为 bean。 Cucumber 会根据需要创建它。

https://github.com/cucumber/cucumber-jvm/tree/main/spring#sharing-state

解决方案是在 cucumber-glue 范围内声明所有 todoRepository 依赖 bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

   
    <bean name="todoRepository" class="xx.java.core.domain.mock.TodoRepositoryMock" scope="cucumber-glue"/>

    <bean name="todoService" class="xx.java.core.domain.todolist.service.impl.TodoListServiceImpl" scope="cucumber-glue">
        <constructor-arg ref="todoRepository"/>
    </bean>

    <bean name="todoStep" class="xx.java.bdd.steps.TodoStep" scope="cucumber-glue">
        <constructor-arg ref="todoService"/>
    </bean>

</beans>