Load different application.yml in SpringBoot Test
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot provides a robust way to manage different configuration profiles using the application.yml file. When conducting tests in a Spring Boot application, it might be necessary to load a different set of configurations tailored specifically for the test environment. This can help simulate conditions that mimic production, or create isolated environments for testing new features. Below is a detailed guide on how to load different application.yml configurations when running Spring Boot tests.
Understanding application.yml in Spring Boot
The application.yml file in a Spring Boot project is used to configure various properties of the application. This central configuration file can handle multiple profiles by specifying properties under different profile headers such as --- in YAML. Here's how a typical application.yml might look:
In the above example, the default profile uses an embedded H2 database, while separate configurations are provided for dev and prod profiles.
Loading a Different application.yml in Tests
Spring Boot provides powerful mechanisms to load different configurations for running tests. These can be done using test-specific application.yml files, using @ActiveProfiles with testing frameworks, or by programmatically specifying properties.
1. Using application-test.yml
A common practice is to create an application-test.yml in the src/test/resources directory, which will automatically be picked up by Spring Boot during testing because it looks specifically for profiles prefixed with application-<profile>.properties or application-<profile>.yml.
Here is how you can structure an application-test.yml:
Spring Boot will use this configuration file while running tests, allowing you to define test-specific configurations.
2. Using the @ActiveProfiles Annotation
You can also explicitly specify the profiles you want to activate during testing using the @ActiveProfiles annotation. This annotation can be applied either at the class level or method level in your test code.
The @ActiveProfiles("test") instructs the Spring Test framework to activate the test profile during execution.
3. Programmatically Specifying Properties
Occasionally, you might want more control over property loading. You can do this programmatically using TestPropertySource or by configuring the Environment.
The @TestPropertySource annotation allows specifying custom configuration files that should be loaded for specific test scenarios.
Table: Key Points on Loading Different application.yml in Tests
| Method | Description | Advantages |
application-test.yml | Automatically detected alternative configuration in src/test/resources. | Simple and intuitive setup. |
@ActiveProfiles("profile") | An explicit way to activate a specific profile. Useful for toggling multiple profiles easily. | Clarity in which profile is active. |
Programmatically with @TestPropertySource | Specifies a custom application.yml file or specific properties to load during tests. | High flexibility and control over test setup. |
Additional Considerations
Using Property Overrides in Tests
You can also use the @TestPropertySource annotation to specify properties directly rather than specifying a file:
Environment-Specific Beans
Spring’s profile feature allows you to define beans specific to certain environments. Using @Profile on beans or configuration classes allows for specific beans to be loaded only when the associated profile is active.
Conclusion
Spring Boot's flexible configuration system including profiles and conditional loading of properties is powerful. With application.yml, developers can ensure their applications are easy to configure across different environments, including production, development, and testing setups. Leveraging these features effectively enhances both the maintainability and robustness of Spring Boot applications.
Related reading
- Loading classes and resources post Java 9
- local variables referenced from an inner class must be final or effectively final
- LocalDate to java.util.Date and vice versa simplest conversion?
- Log4j2 - Unrecognized conversion specifier xwEx starting at position 160 in conversion pattern
- Logging within pytest tests
- Making a mocked method return an argument that was passed to it
- Log4j2 including library name in stacktrace
- log4j Log output of a specific class to a specific appender

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.