Override a property for a single Spring Boot test
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
It is common to need one Spring Boot test class to run with a different configuration value than the rest of the suite. The safest approach is to keep the override local to that test class so the change is obvious, reproducible, and does not leak into unrelated tests.
Use Inline Properties for Simple Static Overrides
For a one-off override, the shortest solution is usually the properties attribute on @SpringBootTest. This keeps the test setup in one place and makes the changed keys visible during review.
Use this when the values are static strings, numbers, or booleans. It is the most readable option for a single test scenario.
Use @TestPropertySource When the Override Set Gets Larger
If a test needs several related properties, @TestPropertySource can be clearer than stuffing everything into one annotation attribute. It also works well when you want to point at a dedicated test property file.
This style is useful when the override values form a small configuration story of their own. The tradeoff is that it adds one more annotation, so it is usually not worth it for only one key.
Use @DynamicPropertySource for Runtime Values
Static annotations stop being convenient once a property depends on something created during test startup, such as a Testcontainers port. That is where @DynamicPropertySource fits.
This keeps runtime-dependent values local to the test class without forcing you to mutate global configuration files.
Verify the Property That Spring Actually Resolved
A property override is not useful if it silently misses the key you intended to change. A small assertion against the Spring Environment or a bound configuration object can save time.
That kind of check catches typos, wrong prefixes, and unexpected precedence issues early.
Choose the Smallest Override Mechanism
Spring offers several ways to change configuration in tests, and using the smallest mechanism that fits the problem keeps the suite predictable:
- use
@SpringBootTest(properties = ...)for a few static values - use
@TestPropertySourcewhen the override set is larger or file-backed - use
@DynamicPropertySourcewhen values are created at runtime - use profiles only when the whole test class needs a coherent alternate environment
If you only need one key changed, editing application-test.yml is usually the wrong move because it changes behavior for every test that loads that profile.
Common Pitfalls
The most common mistake is changing a shared test configuration file for a single scenario and unintentionally affecting dozens of other tests. Another frequent issue is overriding the wrong key because a configuration prefix changed and the test never asserted the resolved value. Teams also mix @ActiveProfiles, inline properties, and @TestPropertySource without understanding precedence, which makes failures hard to explain. Finally, dynamic properties are often used for values that are actually static, which adds complexity without benefit.
Summary
- Keep property overrides local to the one test class that needs them.
- Use inline
@SpringBootTest(properties = ...)for simple static changes. - Reach for
@TestPropertySourcewhen the override set is larger or file-based. - Use
@DynamicPropertySourcefor runtime-generated values such as container ports. - Assert the resolved property value so a typo does not quietly invalidate the test.

