【问题标题】:Spring-Boot Test loading application.propertites wrong - 2.4.2Spring-Boot 测试加载 application.propertites 错误 - 2.4.2
【发布时间】:2021-06-13 04:51:45
【问题描述】:

我使用的是 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;

从一个版本到另一个版本的哪些变化破坏了我的项目?

【问题讨论】:

    标签: spring-boot spring-boot-test


    【解决方案1】:

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

    【讨论】:

    • 我没有使用IDEA编译运行,我使用的是终端的“mvn clean install”命令。
    猜你喜欢
    • 2017-09-06
    • 2016-02-22
    • 2021-03-29
    • 2020-09-26
    • 2019-07-30
    • 2019-05-30
    • 1970-01-01
    • 2021-08-25
    • 2019-01-30
    相关资源
    最近更新 更多