Spring Boot properties in 'application.yml' not loading from JUnit Test
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In the landscape of building Java-based applications using Spring Boot, configuration management plays a critical role. Central to this is Spring Boot's application.yml and application.properties files which allow developers to externalize configuration so they can be easily managed and diversified across different environments. However, a commonly faced issue is the failure of these properties to load during JUnit testing. When properties are not loaded, it leads to incorrect configurations which can yield misleading test results. This article delves into the potential reasons and solutions for this issue.
Understanding the Problem
In Spring Boot, properties in application.yml or application.properties help in managing environment-specific settings, such as database URLs, credentials, port numbers, etc. During application runtime, Spring context loads these properties seamlessly. However, testers often encounter situations where tests running through JUnit do not respect or load these configurations as expected.
The problem typically manifests when running tests with embedded server issues, missing values, or unexpected default settings, indicating that the expected configuration files are not loaded.
Common Causes
1. Incorrect Test Configuration
JUnit tests may sometimes run in isolation without the Spring Application Context, thereby not leveraging the full power of Spring Boot's properties management.
2. Wrong File Location
JUnit tests might not find the property file if it is not located in the expected default location (src/main/resources), causing it to default to some other configuration.
3. Active Profiles Misconfiguration
Not setting the spring.profiles.active property may lead to the default profile being loaded, which might not include the properties required for the test.
4. Annotation Issues
The absence or incorrect use of Spring Boot test annotations can lead to improper context loading.
Solutions and Strategies
Here, we explore various strategies to ensure that your application.yml properties are appropriately loaded during JUnit tests.
Use @SpringBootTest
By annotating your test class with @SpringBootTest, you activate the Spring Boot context, facilitating correct loading of application.yml.
Specify Active Profiles
Use @ActiveProfiles annotation to ensure that the environment profile is correctly set for the test context.
Configs for multiple profiles can be specified in application.yml like:
Check Resource Folder Structure
Ensure your application.yml is present in src/main/resources and, if necessary, a test-specific version in src/test/resources.
Use @TestPropertySource
If you need to specify a particular set of properties only for a single class test, use @TestPropertySource.
Load Properties Programmatically
For advanced control, Spring allows programmatic loading of property sources using a @Configuration class.
Comparison Table
Below is a comparison of strategies used to address the loading of application.yml during JUnit testing:
| Strategy | Description | Pros | Cons |
@SpringBootTest | Full Spring context initialization. | Simple, handles complete context setup. | Slower due to full context initialization. |
@ActiveProfiles | Specifies which profiles to load. | Ensures correct profile properties are loaded. | Requires profile management and may need extra configurations. |
@TestPropertySource | Defines specific YAML or properties files for tests. | Allows test-specific configurations without affecting others. | Adds additional complexity for managing multiple test files. |
| Programmatic Loading | Manual definition inside a configuration class. | Highly customizable and flexible. | Complex and harder to maintain. |
Additional Considerations
- Clean Test Environments: Ensure that no conflicting or unrelated settings affect your testing context.
- Maintain Separate Test Configurations: Use separate test-specific configuration files or database setups.
- Examine Logs: Enable detailed logging to investigate which properties are loading (or not).
In conclusion, while Spring Boot's configuration system is both robust and flexible, ensuring that application.yml properties load correctly during JUnit tests requires careful setup and understanding of the context-loading mechanisms. By utilizing appropriate annotations and structuring test configurations effectively, these challenges can be overcome, leading to more reliable and representative tests.
Related reading
- Spring Boot PSQLException FATAL sorry, too many clients already when running tests
- spring boot rabbitmq MappingJackson2MessageConverter custom object conversion
- Spring Boot read list from yaml using Value or ConfigurationProperties
- Spring Boot redirect HTTP to HTTPS
- Spring boot test configuration
- Spring boot Test fails saying, Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
- Spring Boot Remove Whitelabel Error Page
- Spring Boot requests to re-run your application with 'debug' enabled - how do I?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.