Search code examples
spring-bootspring-cloud

Spring Boot: Override yml list property


For my unit tests I'm trying to override the "property-sources[0]" element of my spring cloud azure configuration so that it is not used during unit tests.

But it seems that the changes done in my application-test.yaml file is not used for the list element and I don't know what the issue, or the correct way to do it, is.

The changes done to the spring.datasource is correctly applied (in memory database is started and the log states: The following 1 profile is active: "test") but the build fails. Because when starting the test, my application tries to connect with the Azure Key Vault defined in the application.yaml:

java.lang.IllegalStateException: Failed to configure KeyVault property source
...
Caused by: java.net.UnknownHostException: my.vault.azure.net

Testclass (Kotlin):

@SpringBootTest
@ExtendWith(MockitoExtension::class)
@ActiveProfiles("test")
internal class BrablTest {...}

application.yaml:

spring:
  datasource:
    url: jdbc:sqlserver://localhost:1433;databaseName=my_db;applicationName=myApp;disableStatementPooling=false;enablePrepareOnFirstPreparedStatementCall=true;statementPoolingCacheSize=20
    username: my_user
    password:
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerXADataSource
  cloud:
    azure:
      keyvault:
        secret:
          property-sources[0]:
            credential:
              managed-identity-enabled: true
            endpoint: https://my.vault.azure.net/

application-test.yaml:

spring:
  datasource:
    url: jdbc:h2:mem:testdb
  cloud:
    azure:
      keyvault:
        secret:
          property-sources[0]:
            enabled: false
            endpoint: https://local.vault.azure.net/

Versions:

  • Java 17.0.4
  • Spring Boot v2.7.4
  • Spring v5.3.23
  • Spring Cloud Azure dependencies 4.4.0

Help would be appreciated.


Solution

  • Thanks @spencergibb for pointing it out. I've read the documentation again and it is written there, that because of the higher priority only the values from "application.yaml" are taken.

    Merging Complex Properties

    When a List is specified in multiple profiles, the one with the highest priority (and only that one) is used.

    Externalized Configuration (order)

    Config data files are considered in the following order:

    1. Application properties packaged inside your jar (application.properties and YAML variants).
    2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).