You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot+Cucumber+JUnit5测试无法加载application-test.properties

问题:Spring Boot+Cucumber+JUnit5环境下测试无法读取test/resources下application-test.properties配置
  • Cucumber可正常识别feature文件,但应用启动失败
  • 业务类中@Value("${connection.string}")注入值为null,导致类实例化失败
  • 相同测试逻辑在JUnit4环境下可正常运行
  • 已确认配置文件真实存在于test/resources/application-test.properties、target/test-classes/application-test.properties路径

环境版本

  • JUnit 5.8.1
  • Spring Boot 2.6.7
  • Cucumber 7.3.3

现有代码配置

1. 应用启动类

@SpringBootApplication
@EntityScan(basePackages = { "com.sams.payout.models.dto" })
@ComponentScan(basePackages = { "com.sams.payout", "com.samcash" })
@Slf4j
public class SampleSpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleSpringApplication.class, args);
        log.info("Application Started");
    }
}

2. 测试上下文配置类

@CucumberContextConfiguration
@SpringBootTest(classes = SampleSpringApplication.class)
@ContextConfiguration(classes = TestHelper.class)
@ActiveProfiles("test")
public class SampleTestApp {
}

3. 功能测试入口类

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameters({
        @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty,json:target/cucumber.json"),
        @ConfigurationParameter(key = JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, value = "long")
})
@ActiveProfiles("test")
public class SampleFT {
}

4. pom.xml相关依赖片段

<dependency>
    <groupId>au.com.dius.pact.provider</groupId>
    <artifactId>junit5</artifactId>
    <version>4.1.36</version>
</dependency>

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <scope>test</scope>
</dependency>

<!--Functional tests -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.9.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit-platform-engine</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>net.masterthought</groupId>
    <artifactId>cucumber-reporting</artifactId>
    <version>${cucumber-reporting.version}</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-core</artifactId>
</dependency>

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite</artifactId>
    <scope>test</scope>
</dependency>

问题原因

核心问题来自两个配置错误:

  1. JUnit 5的Suite执行机制和JUnit 4不同,@ActiveProfiles加在Suite入口类SampleFT上不会被Spring测试上下文识别,Cucumber在JUnit5环境下只会读取@CucumberContextConfiguration标注类上的Spring测试相关注解。
  2. 同时使用@SpringBootTest和@ContextConfiguration时,后者会覆盖前者默认的Spring Boot配置文件加载逻辑,打断test profile的自动配置加载链路,导致框架无法自动识别application-test.properties文件。

修复方案

  1. 移除Suite入口类SampleFT上无效的@ActiveProfiles("test")注解,该注解在Suite类上不会被Spring解析,无需保留。
  2. 修正测试上下文配置类的注解,删除单独的@ContextConfiguration(classes = TestHelper.class),如果需要加载TestHelper配置类,直接把它加入@SpringBootTest的classes数组即可,避免覆盖Spring Boot默认的配置加载规则:
@CucumberContextConfiguration
@SpringBootTest(classes = {SampleSpringApplication.class, TestHelper.class})
@ActiveProfiles("test")
public class SampleTestApp {
}
  1. 如果调整后仍存在加载问题,可以显式指定配置文件路径,在测试上下文配置类上添加@TestPropertySource("classpath:application-test.properties")强制加载目标配置文件。

验证标准:启动测试时观察控制台输出,出现The following 1 profile is active: "test"日志即代表test profile激活成功,配置文件会被正常加载,@Value注入值不会再为null。

内容的提问来源于stack exchange,提问作者Panda Wu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 07:39:29