Override default Spring-Boot application.properties settings in Junit Test
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When developing Spring Boot applications, managing various configurations across different environments is essential. The application.properties or application.yml files are typically used to define these configurations. However, during development, especially in testing phases, overriding these default settings becomes necessary to ensure tests run under conditions that mimic those of different environments or to simply facilitate smoother testing processes.
Understanding the Importance of Environment-Specific Configurations in Testing
In unit testing with JUnit, particularly when you are using Spring Boot’s testing support, you need to configure your tests to use a setup that mirrors the targeted environment as closely as possible without interference from development or production settings. This could involve connecting to a different database, using mock services, or setting specific properties that enable or disable certain features.
Overriding Default Settings in JUnit Tests
Spring Boot provides several ways to override properties during tests. Below are some of the common methods:
- Using
application.propertiesorapplication.ymlin the Test Resources Directory: Create anapplication.propertiesorapplication.ymlfile insidesrc/test/resources. Properties defined here will automatically override the main application’s properties during the test execution phase only. - Using
@TestPropertySourceAnnotation: This annotation provides a way to explicitly define which properties files to use or even directly specify properties within the annotation. It is very flexible as you can define inline properties or point to files.
- Using
@SpringBootTestwithpropertiesAttribute: The@SpringBootTestannotation supports apropertiesattribute which allows you to specify properties directly.
- Using Profiles with
@ActiveProfiles: Spring Profiles determine which properties are to be used based on the active profiles. Using@ActiveProfilesin test classes enables specific profiles for those tests.
Best Practices
- Isolate Test Configuration: Use different configuration files for test contexts to avoid unnecessary overrides in the main application context.
- Use
@MockBeanfor External Services: For external services or components, instead of using real beans, use@MockBeanto prevent actual invocations. - Minimize Inline Overriding: As much as possible, avoid extensive inline property definitions as they can lead to difficult-to-track configurations. Use separate configuration files instead.
Summary Table
| Method | Description | Use Case |
| Test resources | Override using properties file in test resources directory. | General purpose. Easy to manage when project grows. |
@TestPropertySource | Override properties through annotation. | Fine-grained control over properties. Useful for specific test scenarios. |
@SpringBootTest properties | Direct properties override in test annotation. | Quick overrides without additional files. |
@ActiveProfiles | Activate specific profiles for tests. | Environment-specific testing like 'dev', 'test', or 'prod'. |
Conclusion
Overriding default Spring Boot configurations in JUnit tests is a powerful feature that enhances testing flexibility and robustness. Understanding when and how to use different methods effectively ensures that applications behave as expected across all environments, and tests are reliable and meaningful. Whether you manage test-specific properties inline, through separate configuration files, or control active profiles, Spring Boot offers the necessary tools to tailor the testing environment precisely.
Related reading
- Override default Spring-Boot application.properties settings in Junit Test
- Override Java System.currentTimeMillis for testing time sensitive code
- Overriding an Autowired Bean in Unit Tests
- Overriding beans in Integration tests
- Pass deep link into iOS Simulator?
- Passing Moq mock-objects to constructor
- package org.springframework.boot does not exist
- Pagination with DynamoDBMapper Java AWS SDK

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.