Come 2021 and Spring Boot 2.4 the solution I have found is to have 3 properties files
src/main/resources/application.yml
- contains the application's default propssrc/test/resources/application.yml
- sets the profile to 'test', and imports properties from 'main'src/test/resources/application-test.yml
- contains test-specific profiles, which will override 'main'Here is the content of src/test/resources/application.yml
:
# for testing, set default profile to 'test'
spring.profiles.active: "test"
# and import the 'main' properties
spring.config.import: file:src/main/resources/application.yml
For example, if src/main/resources/application.yml
has the content
ip-address: "10.7.0.1"
username: admin
and src/test/resources/application-test.yml
has
ip-address: "999.999.999.999"
run-integration-test: true
Then (assuming there are no other profiles)...
when running tests,
profiles=test
--
ip-address=999.999.999.999
username=admin
run-integration-test=true
and when running the application normally
profiles=none
--
ip-address=10.7.0.1
username=admin
run-integration-test <undefined>