我正在做一个项目,我们有一个组件,其中包括:
- 核
- 外部系统连接器 1
- 外部系统连接器 2
连接器是互斥的(如果连接器 1 处于活动状态,则连接器 2 始终处于非活动状态,反之亦然)。核心和单个连接器在 ApplicationContext 启动时自动连接。实例化哪个连接器取决于 spring 应用程序属性中的值。
我们正在使用 spring-cucumber (v6.2.2) 编写集成测试。对于每个外部系统,我想运行一组黄瓜测试。我在黄瓜场景中使用注释创建了 2 个测试集,这使我可以将连接器 1 和连接器 2 的测试分开。
我遇到的问题是我需要两个测试集以不同的弹簧配置文件运行,所以我可以使用不同的配置。我找不到如何做到这一点。
当前实现(使用单个配置文件):
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>6.2.2</version>
<scope>test</scope>
</dependency>
CucumberConnector1IT.java
package omitted.for.company.rules;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = { "classpath:feature/" },
glue = { "omitted.for.company.rules.cucumber.step" },
plugin = { "pretty", "json:target/cucumber-report/cucumber.json",
"html:target/cucumber-report/cucumber.html" },
tags = "@Connector1 and not @ignore" // tags make sure only applicable tests are run
)
public class CucumberConnector1IT {
}
CucumberConnector2IT.java
package omitted.for.company.rules;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = { "classpath:feature/" },
glue = { "omitted.for.company.rules.cucumber.step" },
plugin = { "pretty", "json:target/cucumber-report/cucumber.json",
"html:target/cucumber-report/cucumber.html" },
tags = "@Connector2 and not @ignore" // tags make sure only applicable tests are run
)
public class CucumberConnector2IT {
}
StepInitializer.java
package omitted.for.company.rules.steps;
import io.cucumber.java.Before;
import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest
@ActiveProfiles("test") // I can only get this to work if the @ActiveProfiles and @CucumberContextConfiguration annotations are on the same class
@CucumberContextConfiguration
public class StepInitializer {
// mock some beans to use in cucumber test
@MockBean
private EventProducer eventProducer;
@MockBean
private RestInterface restInterface;
@Before
public void setup() {
}
}
到目前为止一切正常。但是我现在需要将@ActiveProfiles()
注释放在与@CucumberContextConfiguration
. 如果我能做到这一点,那么我可以用所需的配置文件注释正确的步骤类。
问题是我对弹簧注释的理解不够深入,无法知道哪些可以移动,哪些不能移动。我发现这个例子正是我想要做的(spring-cucumber-profiles repo,注意@ActiveProfiles
这里注释的位置)。不幸的是,它使用的是旧版本的cucumber-spring
(v5.6.0)。该版本还没有@CucumberContextConfiguration
注释,并且根据文档(黄瓜弹簧的发行说明)对弹簧上下文做了一些魔术。我尝试检查示例 repo 并将其升级到 v6.2.2,但无法使其与新版本一起使用。
如果有人在我自己的示例中发现我做错了什么,或者有可能让示例 repo 与版本 6.2.2 一起工作cucumber-spring
,将不胜感激。
提前致谢!:)