0

I've got a spring boot app with source and test hierarchies. In both hierarchies I've got application.yml files with properties.

let's say I've got the following in src application.yml:

settings1:
    setting1: value11
settings2
    setting1: value12

whereeas in the application.yml in test I've got the following:

settings1:
    setting1:testValue11

I want all my tests to know the overridden values from test application.yml and if a value is not present in the test application.yml, the value would be picked up from the src application.yml.

But I want that when my application runs, it knows only the settings from the src application.yml.

How can I solve it? Configuration would be preferable instead of wiring values in the code.

Greg
  • 1,227
  • 5
  • 23
  • 52

1 Answers1

1

Please refer the section 2.7.3. Multi-profile YAML Documents from Spring Boot Reference documentation.

An example with a single application.yml file is as follows.

settings1:
    setting1: 192.168.1.100
settings2:
    setting1: 192.168.1.101
---
spring:
    profiles: test
settings1:
    setting1: 192.168.1.102

and a Test case as follows

@SpringBootTest
@ActiveProfiles("test")
class ApplicationTest {

    @Value("${settings1.setting1}")
    String setting1;

    @Value("${settings2.setting1}")
    String setting2;

    @Test
    void test() {
        System.out.println(setting1);
        System.out.println(setting2);
    }

}

The test case would print

192.168.1.102

192.168.1.101

Edit

For separate/multiple yml files ,

along with application.yml, have a separate application-<profile>.yml (here application-test.yml) to define profile specific properties. The profile specific configuration will take precedence with that profile as active.

Example , define application-test.yml

settings1:
    setting1: 192.168.1.102
R.G
  • 6,436
  • 3
  • 19
  • 28
  • well, your approach is 1 file and not two. besides, I don't want to write over every test class what its active profile is. I want it to be set globally in some configuration. I suspect the solution involves bootstrapping, not sure how though. – Greg Mar 06 '20 at 20:59
  • 1
    For separate files read through till end of the answer . For a global test profile , try [answers](https://stackoverflow.com/q/39690094/4214241) from the SO question. There are multiple ways to boot strap the same – R.G Mar 06 '20 at 23:49
  • as i said I wanted a diff configuration for tests and for executing the app. I wouldn't really change the active config every time I run the app and then run a test – Greg Mar 08 '20 at 10:00
  • the `application.yml` will be the default config and no separate activation/profile required when the app is run. When during tests with a `test`-profile , the `application-test.yml` will be considered with precedence along with `application.yml` entries. If I understand your concern correctly , there will not be any change to the config every time the app is run and when the test is run with a `test` profile the overridden properties are considered. Sorry if this didn't help. – R.G Mar 08 '20 at 11:34