我已经与这个问题斗争了两天,但我想已经没有选择了。我不是 Cucumber 或 Gradle 的专家,但由于我的专业领域很接近(Java/Spring/Maven),我一直在努力将多个基于 Gradle 的 Cucumber 项目更新为所用框架的最新版本。我面临的问题之一是io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.
我通过在每个项目中添加一个这样的类来解决它,如下所示:
@CucumberContextConfiguration
@ContextConfiguration("classpath:cucumber.xml")
public class CucumberSpringConfiguration
{
}
这些配置被添加到步骤定义所在的相同包中,否则它是不可见的,因为步骤定义是通过测试运行器中的注释glue
属性指定的。@CucumberOptions
因此,在我开始将它们作为依赖项组合在一起之前,这些项目都运行正常。想象一下:项目 A 有一些步骤定义,并CucumberSpringConfiguration
定义了其类。项目 B 有自己的步骤定义,并在与其步骤定义相同的包中定义了自己的CucumberSpringConfiguration
类。现在,项目 C 导入 A 和 B 作为其依赖项,以利用步骤定义进行端到端测试。问题就在这里:
io.cucumber.core.backend.CucumberBackendException: Glue class class com.projectA.core.stepdef.CucumberSpringConfiguration and class com.projectB.core.stepdef.CucumberSpringConfiguration are both (meta-)annotated with @CucumberContextConfiguration.
Please ensure only one class configures the spring context
有什么办法可以解决这个问题?我考虑过像这样(在 build.gradle 中)从类路径中排除这些内容,但没有成功:
tasks.withType(Test) {
excludes = ['**/CucumberSpringConfiguration**']
doFirst {
// Get the current classpath
def classpath = sourceSets.test.runtimeClasspath
// Filter out the specific class file I want to exclude
classpath = classpath.filter { file ->
!file.absolutePath.contains("CucumberSpringConfiguration")
}
// Set the modified classpath
setClasspath(files(classpath))
}
}
我试图将依赖项目中的 CucumberSpringConfiguration 移出该步骤定义包,但由于它在测试运行器中的注释glue
参数中使用@CucumberOptions
,因此它会抱怨不需要它。
这里非常需要 Cucumber/Gradle 专家的帮助!提前谢谢您!
项目 A、B 和 C 各自需要一个运行器类和一个在粘合路径上注释的类
@CucumberContextConfiguration
。但它们不应与其他项目共享这些类。因为您正在创建一个库,所以保存的生产代码
src/main
是共享的,但保存的测试代码不是src/test
。因此粘合代码进入src/main
,运行器和带注释的类进入src/test
。