Spring Boot
application.yml
JUnit Test
properties loading
troubleshooting

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.

java
1@SpringBootTest
2public class SomeServiceTest {
3  ...
4}

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.

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

You may see profile settings nested inside application.yml like this:

yaml
1spring:
2  profiles:
3    test:
4      datasource:
5        url: jdbc:h2:mem:testdb

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:

yaml
1app:
2  url: default-url
3---
4spring:
5  config:
6    activate:
7      on-profile: test
8app:
9  url: test-url

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:

java
1@SpringBootTest
2@TestPropertySource(
3  locations = "classpath:application-test.yml")
4public class AnotherServiceTest {
5  ...
6}

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:

java
1public class YamlPropertySourceFactory implements PropertySourceFactory {
2  @Override
3  public PropertySource<?> createPropertySource(String name, EncodedResource resource) {
4    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
5    yaml.setResources(resource.getResource());
6    return new PropertiesPropertySource(resource.getResource().getFilename(), yaml.getObject());
7  }
8}
9
10@SpringBootTest
11@TestPropertySource(locations = "classpath:custom.yml", factory = YamlPropertySourceFactory.class)
12public class AnotherServiceTest {
13  ...
14}

Load the YAML programmatically

For full control over @Value placeholders, load the YAML yourself in a test configuration class:

java
1@TestConfiguration
2public class TestConfig {
3  
4  @Bean
5  public static PropertySourcesPlaceholderConfigurer properties() {
6    final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
7    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
8    yaml.setResources(new ClassPathResource("application.yml"));
9    pspc.setProperties(yaml.getObject());
10    return pspc;
11  }
12}

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

StrategyDescriptionProsCons
@SpringBootTestFull Spring context initialization.Simple, handles complete context setup.Slower due to full context initialization.
@ActiveProfilesSpecifies which profiles to load.Ensures correct profile properties are loaded.Requires profile management and may need extra configurations.
@TestPropertySourceDefines specific properties files for tests.Allows test-specific configurations without affecting others.Does not load YAML by default; more test files to manage.
Programmatic LoadingManual 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.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms