首页 > 解决方案 > 使用 cucumber-spring 合并属性文件

问题描述

我们正在使用 cucumber + selenium 来促进 e2e 测试。我们正在使用以下依赖项来利用 spring 依赖项注入。

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>6.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.6.RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.6.RELEASE</version>
    <scope>provided</scope>
</dependency>

然后我们使用这两个类来配置spring context

@ContextConfiguration(classes = SpringContextConfig.class)
@CucumberContextConfiguration
public class CucumberSpringConfiguration {
}
@PropertySource("application.properties")
@ComponentScan("com.example.e2e")
public class SpringContextConfig {
}

正如我们所看到的,我们的属性源被明确定义为指向给定的文件。有没有办法实现 Spring Boot 对配置文件和属性文件所做的“魔术”?

例如,当指定-Dspring.profiles.active=dev合并 application-dev.propertiesapplication.properties给出最终的上下文属性时,我会期待春天。

标签: javaspringspring-bootcucumbercucumber-spring

解决方案


正如我们所看到的,我们的属性源被明确定义为指向给定的文件。有没有办法实现 Spring Boot 对配置文件和属性文件所做的“魔术”?

Profile 特定的配置文件是 Spring Boot 的一项功能。因此,如果您使用的是 Spring boot,这应该足以让活动配置文件被拾取:

@SpringBootTest
@CucumberContextConfiguration
public class CucumberSpringConfiguration {
}

但是,从您的依赖项来看,您似乎没有使用 Spring Boot。没有其他(简单的)方法可以使用配置文件特定的配置文件。考虑使您的应用程序成为 Spring Boot 应用程序。


推荐阅读