2

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

  • 外部系统连接器 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,将不胜感激。

提前致谢!:)

4

1 回答 1

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

这样我仍然可以使用不同的弹簧轮廓:)。

于 2020-11-12T17:24:58.940 回答