zonkyio / embedded-database-spring-test

A library for creating isolated embedded databases for Spring-powered integration tests.
Apache License 2.0
408 stars 37 forks source link

Spring boot YML configs are not read #153

Closed ValdasK closed 3 years ago

ValdasK commented 3 years ago

I am unable to change image which is used for tests when using Spring Boot 2.4.3 + YML configuration.

If I add

zonky:
  test:
    database:
      postgres:
        docker:
          image: postgres:12-alpine

into application.yml, it get's ignored, and adding breakpoint in DockerPostgresDatabaseProvider

String dockerImage = environment.getProperty("zonky.test.database.postgres.docker.image", "postgres:11-alpine");

shows that dockerImage is still "postgres:11-alpine". I think this could be caused that Environment does not parse YML, and only works with .properties? Would it not be possible to use @Value to extract configurations instead?

tomix26 commented 3 years ago

Environment should be able to read values from yaml files. We use it this way and it works well.

Could you please check it directly in your test class? Just inject Environment bean to your test class, invoke environment.getProperty("...") and compare the result with a value in field annotated with @Value("${zonky.test.database.postgres.docker.image}").

tomix26 commented 3 years ago

Something like this:

@RunWith(SpringRunner.class)
public class ConfigurationPropertiesIntegrationTest {

    @Autowired
    private Environment environment;

    @Value("${zonky.test.database.postgres.docker.image}")
    private String valueDockerImage;

    @Test
    public void testConfigurationProperties() throws Exception {
        String envDockerImage = environment.getProperty("zonky.test.database.postgres.docker.image", "postgres:11-alpine");
        // compare or print envDockerImage and valueDockerImage variables
    }
}
ValdasK commented 3 years ago

Thanks for swift reply; it turns out that none of configs were actually parsed!

Added context initialize like

@ContextConfiguration(
  initializers = ConfigDataApplicationContextInitializer.class,
  classes = {...})

and it started to populate config values (and that made this setting work correctly).

It's really confusing what you have to add/what you can omit once you start customizing things...

If this sounds like reasonable thing to do; you can close issue.

tomix26 commented 3 years ago

Yes, of course, ConfigFileApplicationListener must be registered properly. Otherwise it can't work. Ok, I consider it solved and I'm closing the issue.