gpt4 book ai didi

spring-boot - Spring Cucumber ActiveProfiles 注释不适用于 CucumberContext

转载 作者:行者123 更新时间:2023-12-04 08:36:07 28 4
gpt4 key购买 nike

我正在做一个项目,我们有一个组件,其中包括:

  • 核心
  • 连接到外部系统 1
  • 连接到外部系统 2

  • 连接器是互斥的(如果连接器 1 处于事件状态,则连接器 2 始终处于非事件状态,反之亦然)。核心和单个连接器在 ApplicationContext 启动时 Autowiring 。实例化哪个连接器取决于 spring 应用程序属性中的值。
    我们正在使用 spring-cucumber (v6.2.2) 编写集成测试。对于每个外部系统,我想运行一组 cucumber 测试。我已经使用 cucumber 场景上的注释创建了 2 个测试集,这允许我将连接器 1 和连接器 2 的测试分开。
    我遇到的问题是我需要两个测试集以不同的 spring 配置文件运行,所以我可以使用不同的配置。我找不到如何做到这一点。
    当前实现(使用单个配置文件):
    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 注释,无法知道哪些可以移动,哪些不能。我发现这个例子正是我想要做的( spring-cucumber-profiles reponotice the location of the @ActiveProfiles annotation here )。不幸的是,它使用了旧版本的 cucumber-spring (v5.6.0)。该版本还没有 @CucumberContextConfiguration注释并根据文档( Release notes of cucumber-spring )对 spring 上下文做了一些魔法。我尝试检查示例 repo 并将其升级到 v6.2.2,但无法使用新版本。
    如果有人在我自己的示例中发现我做错了什么,或者有可能使示例存储库与 cucumber-spring 的 6.2.2 版一起使用将不胜感激。
    提前致谢! :)

    最佳答案

    我通过稍微分离包并创建单独的 StepInitializer 解决了这个问题。两个测试集的类。
    当前设置:
    测试运行器:

    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/" },
    extraGlue = { "omitted.for.company.rules.cucumber.step.common" }, // used extraGlue instead of glue
    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 {
    }
    上下文配置:
    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", "test-connector1" })
    @CucumberContextConfiguration
    public class Connector1StepInitializer {

    // mock some beans to use in cucumber test
    @MockBean
    private EventProducer eventProducer;
    @MockBean
    private RestInterface restInterface;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private Environment environment;

    @Before
    public void setup() {
    assertThat(applicationContext).isNotNull();
    assertThat(environment.getActiveProfiles()).containsOnly("test","test-connector1");
    }
    }
    两个连接器/测试运行器都有自己的运行器类和它们自己的 ContextConfiguration 类。
    包含 @CucumberContextConfiguration 的类非常重要。注释不在共享胶水包中(在 extraGlue 注释中的 @CucumberOptions 属性中提供)。
    包结构如下所示:
    ├───common
    │ └───step // Contains shared steps. This path should be in the 'extraGlue' field of the runner classes
    ├───connector1
    │ │ CucumberConnector1IT.java // Runner 1
    │ └───step
    │ Connector1Steps.java // Specific steps
    │ Connector1StepInitializer.java // has @ActiveProfiles and @CucumberContextConfiguration annotations, use to mock beans
    └───connector2
    │ CucumberConnector1IT.java // Runner 2
    └───step
    Connector2Steps.java // Specific steps
    Connector2StepInitializer.java // has @ActiveProfiles and @CucumberContextConfiguration annotations, use to mock beans
    这样我仍然可以使用不同的 Spring 轮廓:)。

    关于spring-boot - Spring Cucumber ActiveProfiles 注释不适用于 CucumberContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64791136/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com