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.

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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.
For separate files read through till end of the answer . For a global test profile , try answers from the SO question. There are multiple ways to boot strap the same
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.