Home > database >  Spring Boot integration test for validating nested configuration properties
Spring Boot integration test for validating nested configuration properties

Time:12-18

Using Spring Boot 2.6 I stumble across the following problem.

I defined a ConfigurationProperties class which maps the properties from application.yml. The configuration also includes validation annotations.

A missing property like my.prefix.sample.username prevents the applications from being started which is fine.

In a basic integration test however, this does not work. I would expect the below test to fail if my.prefix.sample.password was missing in src/test/resources/application-integration-test.yml. But it passes, so it seems as if the validation does really not kick in. If however I remove my.prefix.sample.url, then the test fails.

// integration test
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("integration-test")
public class ApplicationIT {

    @Test
    void contextLoads() {
    }
}

// the ConfigurationProperties classes

@Configuration
@ConfigurationProperties(prefix = "my.prefix")
@Getter
@Setter
@Validated
public class ApplicationConfiguration {

    @NotBlank
    @URL
    private String url;

    @NestedConfigurationProperty
    @Valid
    private SampleConfiguration sample;
}

@Getter
@Setter
public class SampleConfiguration {

    @NotBlank
    private String username;

    @NotBlank
    private String password;
}

CodePudding user response:

As you're inheriting any property from your default application profile (application.properties inside src/main/resources), you can place an empty application.properties file inside src/test/resources to avoid inheriting any value.

  • Related