SpringBoot
application.yml
testing
configuration
Java

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.

Browse interview questions

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:

yaml
1server:
2  port: 8080
3
4spring:
5  datasource:
6    url: jdbc:h2:mem:testdb
7    username: sa
8    password:
9
10---
11spring:
12  profiles: dev
13  datasource:
14    url: jdbc:postgresql://localhost:5432/devdb
15    username: devuser
16    password: devpassword
17
18---
19spring:
20  profiles: prod
21  datasource:
22    url: jdbc:postgresql://localhost:5432/proddb
23    username: produser
24    password: prodpassword

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:

yaml
1server:
2  port: 8081
3
4spring:
5  datasource:
6    url: jdbc:h2:mem:testdb
7    username: testuser
8    password: testpassword

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.

java
1import org.junit.jupiter.api.Test;
2import org.springframework.boot.test.context.SpringBootTest;
3import org.springframework.test.context.ActiveProfiles;
4
5@SpringBootTest
6@ActiveProfiles("test")
7public class ExampleTest {
8
9  @Test
10  public void contextLoads() {
11    // context loading test
12  }
13}

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.

java
1import org.junit.jupiter.api.Test;
2import org.springframework.boot.test.context.SpringBootTest;
3import org.springframework.test.context.TestPropertySource;
4
5@SpringBootTest
6@TestPropertySource(locations = "classpath:application-test.yml")
7public class AnotherExampleTest {
8
9  @Test
10  public void test() {
11    // test code
12  }
13}

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

MethodDescriptionAdvantages
application-test.ymlAutomatically 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 @TestPropertySourceSpecifies 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:

java
1@SpringBootTest
2@TestPropertySource(properties = {
3  "spring.datasource.url=jdbc:h2:mem:customdb",
4  "spring.datasource.username=customuser"
5})
6public class DirectPropertiesTest {
7
8  @Test
9  public void customDbTest() {
10    // Test using overridden DB properties
11  }
12}

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.

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.context.annotation.Profile;
4
5@Configuration
6public class DataSourceConfig {
7
8  @Bean
9  @Profile("prod")
10  public DataSource prodDataSource() {
11    // Return production datasource
12  }
13}

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
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
Track 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.

Browse interview questions

All Rights Reserved.