Spring-Boot 测试加载 application.properites 错误 - 2.4.2

问题描述

我使用的是 Spring-Boot 2.2.4.RELEASE 版本,我决定升级到 2.4.2 版本,因为它不是发布版本。我的问题开始是因为我有两个配置文件一个使用 configserver,另一个使用内存数据库配置为类测试。

src/main/resources/application.properties:

...
spring.profiles.active=@activatedProperties@
spring.config.import=optional:configserver:@url@

src/test/resources/application.yml:

spring:
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    show-sql: true
    properties:
      hibernate.format_sql: true
      ....
  datasource:
     driver-class-name: org.h2.Driver
     platform: h2
     name: CUP_orchestraTOR
     url: jdbc:h2:mem:CUP_orchestraTOR;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS CUP_orchestraTOR
     initializaion-mode: embedded
     username: sa
     password: 

在更新之前,当我运行 Spring-Boot 测试类时,它总是采用正确的配置文件,但现在它正在获取 src/main/resources/application.properties 进行测试:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.closeupinternational.cpoprocesscontrol.test.WorkflowServiceTest
18:39:28.943 [main] ERROR o.s.boot.SpringApplication - Application run Failed
java.lang.IllegalStateException: Unable to load config data from 'optional:configserver:http://localhost:8888'

工作流服务测试:

@SpringBoottest(properties = "spring.cloud.config.enabled=false")
@TestPropertySource("classpath:application.yml")
@Slf4j
public class WorkflowServiceTest {

    @MockBean
    private SenderConfig senderConfig;

一个版本到另一个版本的改变是什么破坏了我的项目?

解决方法

将 application.yml 更改为 application.properties,因为 TestPropertySource 注释仅适用于 .properties 文件。