Spring Boot application properties or yml in test resources is a code smell

In several projects I've seen application.properties or application.yml files in test resources.
These files were almost exactly the same as files in main resources.
Each time developer adds a new configuration to file in main resources, he/she needs to update a file in test resources.
It's definitely a code smell.

Solution before Spring Boot 2.6

To avoid duplicate configuration files you can create a file src/test/resources/application-test.properties or src/test/resources/application-test.yml.
Override or add here properties needs only for tests. During tests Spring Boot will also load properties from main resources.
Now you can activate test profile in test using ActiveProfiles annotation.

1@SpringBootTest
2@ActiveProfiles("test")
3public class SomeExampleTestIT {
4
5}

Solution after Spring Boot 2.6

Spring Boot 2.6 changed the way1 how it loads configuration files.
Now it loads from resources root and than from directory config.
So you can create files

  • src/main/resources/application.properties - this properties is always loading
  • src/test/resources/config/application.properties - this properties is loading only in tests

This approach doesn't need additional profile.

Translations: