in Coding

Testing @ConfigurationProperties in Spring Boot

*** Update ***
I updated the example on GitHub to Spring 2.5.X! The latest post on this topic is at

I recently worked on a library for using AWS Kinesis in Spring Boot. As many other libraries, this particular one provided a powerful configuration. To implement the configuration, we used Spring Boot’s @ConfigurationProperties (as described here). This article gives some insights on how we did our testing.

You can find the source code on GitHub:

The configuration

Our configuration wasn’t very complicated – it was just large. Below you can see a simplified example with two properties. In reality many more followed. Some properties are mandatory while others are optional. Additionally some properties have default values which can be overwritten.

(View on GitHub)

The happy path

We started testing with the happy path. The test was pretty simple and worked well. You can see it below.

(View on GitHub)

Different variations

Our next step was to test different variations of the configuration. Since some properties are optional, we wanted to test what will happen if they are missing. So we wrote another test:

(View on GitHub)

The problem

However, there was a problem with this approach: we needed to write a complete test class for each scenario., and so on. Each variation would require us to create a new class and a new application-xxx.yml file. That wasn’t to end well…

Programmatically loading @ConfigurationProperties

Our solution was to load the @ConfigurationProperties programmatically instead of just wiring them with Spring. This gave us the possibility to modify them in each test. To make life even easier we wrote a ConfigurationPropertiesBuilder for our test cases. You can see the result below. We accomplished to test a lot of cases in a quite simple manner in a single test:

(View on GitHub)

The builder we used to set-up our properties looks like below. It provides different options in order to build the properties. For example you can set properties from a YAML file or programmatically.

(View on GitHub)


In order to test the validation, we needed to add some dependencies to our project. Depending on your setup, this might be needed or not.

Example on GitHub

I have published this example on GitHub:

Best regards,

Write a Comment


  1. With Spring Boot 2 PropertiesConfigurationFactory not exists any more. Use the new Binder instead:
    Binder binder = new Binder(propertySources);
    binder.bind(prefix, MyConfigurationProperties.class).get();

  2. An example how to load properties with Spring Boot 2 (in groovy, so for java you need to replace def with correct types):
    private MyConfigurationProperties loadYamlProperties(String fileName) {
    def propertiesFactoryBean = new YamlPropertiesFactoryBean()
    propertiesFactoryBean.setResources(new ClassPathResource(fileName))
    def properties = propertiesFactoryBean.getObject()
    def propertySource = ConfigurationPropertySources.from(new PropertiesPropertySource(‘properties’, properties))
    def binder = new Binder(propertySource)
    binder.bind(prefix, Bindable.of(MyConfigurationProperties), new ValidationBindHandler(localValidatorFactoryBean)).get()

  3. How should we handle this in Spring boot 2? The PropertiesConfigurationFactory is removed and replaced with Binder