Search code examples
spring-bootspring-boot-test

Spring-Boot Test loading application.propertites wrong - 2.4.2


I was using Spring-Boot 2.2.4.RELEASE version and I decided to upgrade to 2.4.2 version since is not a released version. My problem started because I have two configuration file, one that I use configserver and another that I have a in memory database configured to class test.

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: 

Before the update when I ran the Spring-Boot test class, it always took the correct configuration file, but now it is getting src/main/resources/application.properties for test:

[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'

WorkflowServiceTest:

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

    @MockBean
    private SenderConfig senderConfig;

What changed from one version to another that broke my project?


Solution

  • Change application.yml to application.properties as the TestPropertySource annotation only works for .properties files.