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.
Introduction
When working with Spring Boot applications, developers often need to test certain functionalities diverging from the application's default configurations. JUnit, a popular testing framework for Java, allows developers to write unit tests to ensure the correctness of their applications. In Spring Boot tests, overriding properties in application.properties is a common necessity. This article explores how to override default Spring Boot application properties in JUnit tests, providing detailed explanations and examples.
Spring Boot Testing Overview
Spring Boot provides an integrated test support through Spring's @SpringBootTest and other annotations that render tests more straightforward and effective:
@SpringBootTest: This annotation is used to load the complete application context.@TestPropertySource: This annotation is used to override properties at test level.@ValueandEnvironment: These can be used to inject values or programmatically access property values.
Overriding Application Properties
When writing tests, there might be scenarios where you want the application to behave differently than its default mode. For example, you might want to set an in-memory database instead of an external database or use mock services. Here are methods to override properties:
Using @TestPropertySource
One of the simplest ways to define property overrides in Spring Boot tests using JUnit is the @TestPropertySource annotation. This annotation allows for easy overriding of properties.
Using @SpringBootTest with properties Attribute
If you're using @SpringBootTest, it directly supports setting inline properties, enabling overriding without an additional annotation:
Using SpringBootTest.WebEnvironment
When testing specific layers of your application, such as when running tests as different web environments, you might need to override web-related properties.
Creating a Custom application-test.properties File
Rather than inline properties, you can provide a separate application-test.properties file to the @TestPropertySource annotation:
Direct Property Setting via Programmatic Means
Sometimes properties might need programmatic approaches. Using Spring's Environment or setting up a DynamicPropertySource in conjunction with @TestConfiguration or @DynamicPropertySource may be the way to go.
Best Practices and Considerations
- Clear Test Configuration: Keep test configurations separate from application configurations. Use specific properties for testing (
application-test.properties). - Environment Specific: Ensure property overrides are relevant to the specific tests being run. Avoid unnecessary configurations that don't influence the test scenarios.
- Isolation and Independence: Tests should not depend on particular environments, ensure they're isolated and independently executable.
- Avoid Hardcoding Values in Tests: Use constants or reference utility methods to set properties dynamically, when possible.
Property Override Summary
| Method | Description |
@TestPropertySource | Overrides properties using properties or a separate file. |
@SpringBootTest(properties) | Inline property overrides within the annotation. |
SpringBootTest.WebEnvironment | Configures web environment properties for test scenarios. |
@DynamicPropertySource | Programmatically configures properties in a dynamic manner. |
Conclusion
Understanding how to override Spring Boot application properties in JUnit tests can enhance the reliability and coverage of your test suite. Whether using annotations or programmatically setting configurations, choosing the right method based on your test goals ensures streamlined, specific, and effective test cases.
Related reading
- Override Java System.currentTimeMillis for testing time sensitive code
- Overriding an Autowired Bean in Unit Tests
- Overriding beans in Integration tests
- package org.springframework.boot does not exist
- Pass deep link into iOS Simulator?
- Passing Moq mock-objects to constructor
- Pagination with DynamoDBMapper Java AWS SDK
- 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.