Spring Boot application.yml properties not loading in a JUnit test
Codemia Knowledge Hub ·
Properties from application.yml are only loaded when the test starts a Spring Boot application context, so the usual fix is annotating the test with @SpringBootTest. For test-specific YAML, put an application-test.yml on the classpath and activate it with @ActiveProfiles("test"). Note that @TestPropertySource does not read YAML files with its default property source factory, which is a common reason the file appears to be ignored.
When properties fail to load, tests run with missing values or unexpected defaults (wrong database URLs, ports, credentials), and the results can be misleading.
Why the properties are not loaded
The test runs without a Spring context
A plain JUnit test runs in isolation, without the Spring application context, so none of Spring Boot's property handling happens.
The file is not where Spring looks
If the property file is not in the expected location (src/main/resources, or src/test/resources for a test-specific copy), the test falls back to some other configuration.
The wrong profile is active
If spring.profiles.active is not set, the default profile is used, and it may not contain the properties the test needs.
Missing or incorrect test annotations
Leaving out Spring Boot test annotations, or using them incorrectly, leads to an improperly loaded context.
Fixes
Start the context with @SpringBootTest
Annotating the test class with @SpringBootTest starts the Spring Boot context, which loads application.yml.
With JUnit 5 that is all you need, because @SpringBootTest already includes the Spring extension. Add @RunWith(SpringRunner.class) only on JUnit 4 tests.
Activate a test profile with @ActiveProfiles
@ActiveProfiles sets the profile for the test context.
You may see profile settings nested inside application.yml like this:
That nesting does not define a profile; it just creates a property named spring.profiles.test.datasource.url. The reliable way to supply test profile values is a separate application-test.yml file, which @ActiveProfiles("test") picks up.
To keep everything in one file, use a multi-document YAML file instead. The section after --- applies only when the test profile is active:
With @ActiveProfiles("test") the test sees app.url=test-url; without it, default-url.
Check the resource folders
Make sure application.yml is in src/main/resources and, if you need one, a test-specific version is in src/test/resources.
A src/test/resources/application.yml does not add to the main file, it replaces it. Both end up at classpath:application.yml, and the test copy comes first on the test classpath, so properties defined only in the main file come back null. Name the test file application-test.yml (with @ActiveProfiles("test")) if you want it layered on top of the main one.
Know the limits of @TestPropertySource
@TestPropertySource sets properties for a single test class, but by default it only understands .properties files (and their XML variant). Pointing it at a YAML file, as below, does not load the YAML:
The file is parsed as .properties, so nested YAML keys end up missing. Use a .properties file with @TestPropertySource, or keep YAML and use the profile approach above.
On Spring Framework 6.1+ (Spring Boot 3.2+) @TestPropertySource also accepts a factory, so you can plug in a small YAML factory:
Load the YAML programmatically
For full control over @Value placeholders, load the YAML yourself in a test configuration class:
This only resolves ${...} placeholders in @Value and bean definitions. The values are not added to the Environment, so env.getProperty(...) and @ConfigurationProperties do not see them.
Comparing the approaches
| 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 properties files for tests. | Allows test-specific configurations without affecting others. | Does not load YAML by default; more test files to manage. |
| Programmatic Loading | Manual definition inside a configuration class. | Highly customizable and flexible. | Complex and harder to maintain. |
Keeping test configuration predictable
- Make sure no conflicting or unrelated settings leak into the test context.
- Keep separate test configuration files or test databases.
- Turn on detailed logging to see which properties are loaded and which are not.
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?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.