首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring注释不使用CucumberContext

Spring注释不使用CucumberContext
EN

Stack Overflow用户
提问于 2020-11-11 17:18:29
回答 1查看 1.7K关注 0票数 2

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

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

连接器是相互导出的(如果connector1是活动的,则connector2总是不活动的,反之亦然)。在启动ApplicationContext时,核心和单个连接器是自动启动的。实例化哪个连接器是基于spring应用程序属性中的值。

我们正在使用Spring-黄瓜编写集成测试(v6.2.2)。对于每个外部系统,我想运行一组黄瓜测试。我在黄瓜场景中使用注释创建了两个测试集,这允许我分离connector1和connector2的测试。

我遇到的问题是,我需要两个测试集以不同的spring配置文件运行,所以我可以使用不同的配置。我找不到该怎么做。

目前的实施情况(只有一个简介):

pom.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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不同的类上。如果我能做到这一点,那么我就可以用所需的配置文件注释正确的step类。

问题是,我不太了解spring注释,不知道哪些注释可以移动,哪些注释不能。我找到了这个我想要做的事情的例子(春黄瓜型材回购注释在这里)。不幸的是,它使用的是较早版本的cucumber-spring (v5.6.0)。该版本还没有@CucumberContextConfiguration注释,并且根据文档(黄瓜-弹簧释放说明)对spring上下文做了一些神奇的工作。我试着签出示例repo并将其升级到v6.2.2,但无法使它与新版本一起工作。

如果有人注意到我在自己的示例中做错了什么,或者有可能让示例回购使用6.2.2版本的cucumber-spring,这将是非常值得赞赏的。

谢谢!)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-12 17:24:58

我已经解决了这个问题,对包进行了一些分离,并为两个测试集创建了单独的StepInitializer类。

当前设置:

测试跑步者:

代码语言:javascript
复制
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 {
}

上下文配置:

代码语言:javascript
复制
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");
    }
}

连接器/测试运行程序都有自己的runner类和自己的ContextConfiguration类。

非常重要的是,包含@CucumberContextConfiguration注释的类不存在于共享胶包中(如@CucumberOptions注释中的extraGlue属性中提供的那样)。

包结构如下所示:

代码语言:javascript
复制
├───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配置文件:)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64791136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档