Overriding Properties in @SpringBootTest
Fast recall
Quick revision
Core answer
@SpringBootTest(properties = ...) for inline test properties, @TestPropertySource for declared test sources, @ActiveProfiles("test") to activate profile-specific configuration, or @DynamicPropertySource for values created at test time. Spring has a defined property-source precedence, so use the mechanism whose scope and precedence match the test instead of assuming every test source is equivalent.Practical knowledge of test configuration management, especially for changing database URLs, ports, or feature flags without touching production config
Creating multiple application-test.yml files without understanding inline property overrides, or not knowing that test properties have higher precedence than application.yml
Mention @DynamicPropertySource for runtime values such as container ports and avoid claiming a simple universal precedence between combined inline test mechanisms
Interview-ready guide
Interview answer
I use @SpringBootTest properties for a class-specific inline value, @TestPropertySource for a declared test source, @ActiveProfiles to select shared profile configuration, and @DynamicPropertySource for runtime values such as a container URL or port. These feed Spring's Environment and override normal application sources according to the documented property order. I avoid setting the same key in several test mechanisms because the test becomes hard to read and may use a different context-cache key. I also remember that changing a URL only changes configuration; it does not itself start or isolate the database or service.
Overriding Properties in @SpringBootTest
Direct Answer
Use @SpringBootTest(properties = ...) for inline test properties, @TestPropertySource for declared test sources, @ActiveProfiles("test") to activate profile-specific configuration, or @DynamicPropertySource for values created at test time. Spring has a defined property-source precedence, so use the mechanism whose scope and precedence match the test instead of assuming every test source is equivalent.
Code Example
@SpringBootTest(properties = {
"spring.datasource.url=jdbc:h2:mem:testdb",
"app.feature.notifications=false"
})
class OrderServiceTest {
@Autowired private OrderService orderService;
@Test
void testOrderCreation() {
// H2 in-memory DB and notifications disabled for this test class
}
}
@SpringBootTest
@TestPropertySource(properties = {
"server.port=0", // random port
"app.external.api=http://localhost:8081"
})
class PaymentControllerTest {
// @TestPropertySource has higher precedence than the properties attribute
}
@SpringBootTest
@ActiveProfiles("test")
class UserRepositoryTest {
// loads application-test.yml automatically
}
Common Mistakes & Pitfalls
- Duplicating all production configuration: Override only what the test intentionally changes.
- Assuming braces are mandatory: Java permits the single-element shorthand for an array-valued annotation attribute, so both
properties = "a=b"andproperties = {"a=b"}are valid. - Setting one key in several test sources: Even when precedence is documented, the result is harder to understand and cache reuse may change. Keep one obvious owner.
- Hardcoding runtime ports: Register container-generated values with @DynamicPropertySource.
Check your understanding
Answer these without looking back at the article.
- What is @ActiveProfiles and how does Spring Boot load profile-specific property files?
- Can you use @SpringBootTest with an in-memory H2 database for the full context?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.