0

Our main application is using Config Server for storing all of its configuration, but the tests were so far configured using a copy of that config as application.yml and overridden properties in application.properties plugged into the test class using

@TestPropertySource(locations= { "classpath:application.properties" })

I have converted the properties file into yml, to keep consistency, since we were moving on to that format everywhere. Now the unit tests won't run at all due to:

Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set

But this property is present in the new converted application-test.yml. If I move the property to the application.yml then the error just moves on to the next missing property

Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"

I tried switching to the config server as the main property source, but the overridden properties are still ignored. I have also tried putting both configs in @TestPropertySource(locations= in order in which they should be overridden, but also no luck.

Here is my test class config:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {LoaderApplication.class})
@TestPropertySource(locations= { "classpath:application-test.yml" })

application.yml:

spring:
  polling:
    file:
      cron: 0/20 * * * * MON-FRI
      maxMessagesPerPoll: 1
 # a lot of polling configs, some ftp configuration and credentials

application-test.yml

spring:
  batch:
    job:
      enabled: false
  cloud:
    aws:
      s3:
        enabled: false
  config:
    import: 'optional:configserver:'
  datasource:
    driverClassName: org.h2.Driver
    password: ''
    url: jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;MV_STORE=FALSE;MODE=MSSQLServer
    username: sa
  flyway:
    baseline-on-migrate: true
    baselineOnMigrate: true
    enabled: false
    ignoreMissingMigrations: true
    locations: classpath:db/migration/{vendor}
    table: ${spring.application.name}_schema_version
    url: jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;MV_STORE=FALSE;MODE=MSSQLServer

  h2:
    console:
      enabled: true
      path: /h2-console
      settings:
        web-allow-others: true
  jpa:
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        default_schema: renewals
  location:
    inputPath: src/main/resources/tests/input
    outputPathFailure: src/main/resources/tests/output/failure
    outputPathSuccess: src/main/resources/tests/output/success

cn:
  app:
    datasource-populate:
      enabled: true

jasypt:
  encryptor:
    password: 

loader:
  scope: TEST

logging:
  level:
    org:
      springframework: ERROR
    root: ERROR

vendor: h2
kacpr
  • 440
  • 1
  • 8
  • 28
  • 1
    You can try `ActiveProfile("test")` instead of `@TestPropertySource(locations= { "classpath:application-test.yml" })`. Can you update your post with the `yaml` file. – Harry Coder Feb 26 '23 at 16:02
  • Thanks. Somehow this does work! I didn't mention but the chain of properties is a bit longer, as well as `application.yml` we have an environment specific `application-local.yml` which just adds more properties to the main application. And of finally the `application-test.yml` which overrides what it needs for the tests. So my annotation was `@ActiveProfiles(profiles = {"local", "test"})`. Anyway, if you want to put it into an answer I'll be happy to accept it. – kacpr Feb 26 '23 at 16:35
  • 1
    When putting `@ActiveProfiles(profiles = {"local", "test"})` does it work? – Harry Coder Feb 26 '23 at 19:14
  • @HarryCoder yes it does – kacpr Feb 27 '23 at 08:53
  • 1
    `@PropertySource` and `@TestPropertySource` don't work with yaml format - only with .properties. – ILya Cyclone Feb 27 '23 at 11:10

0 Answers0