1

I have the following property list in my application.yml:

foo: 
  bar: 
    - 
      id: baz
      item: value
    // ...

Then I want to overwrite item value in tests using @DynamicPropertySource:

    @DynamicPropertySource
    @JvmStatic
    @Suppress("unused")
    fun setupProperties(registry: DynamicPropertyRegistry) {
        registry.add("foo.bar[0].item") { "new value" }
    }

But during the tests, I got all other properties set to nulls, with one element in bar array.

I guess that I'm not referring correctly to map entry in yaml file. I wonder how I can do that?

pixel
  • 24,905
  • 36
  • 149
  • 251
  • `foo.bar[0].item` looks correct to me, at least regarding how Spring's `YamlPropertiesFactoryBean` would map that to a property name. Maybe it would be better if you create a sample project (preferably in Java instead of Kotlin) somewhere that it can be checked out (e.g., GitHub). – Sam Brannen Apr 23 '20 at 16:48
  • @SamBrannen I created sample project with tests that show unusual behavior: https://github.com/kkocel/dynamicproperties – pixel Apr 24 '20 at 09:46
  • @SamBrannen created an issue on GitHub as well: https://github.com/spring-projects/spring-framework/issues/24974 – pixel Apr 25 '20 at 17:43

1 Answers1

1

It turns out that Spring Boot documentation states clearly:

When lists are configured in more than one place, overriding works by replacing the entire list.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-complex-type-merge

This effectively means that I need to provide whole list item:

@DynamicPropertySource
@JvmStatic
@Suppress("unused")
fun setupProperties(registry: DynamicPropertyRegistry) {
    registry.add("foo.bar[0].id") { "new baz" }
    registry.add("foo.bar[0].item") { "new value" }
    // ...
}
pixel
  • 24,905
  • 36
  • 149
  • 251