@TestPropertySource is a Spring Framework test annotation that adds property files or inline key-value pairs to the Environment of the ApplicationContext that Spring creates for your integration test. Its properties have a higher precedence than system properties, environment variables and application.properties, so a test can override any production setting without touching the real configuration.
You put it on a test class, and the Spring TestContext Framework applies it when it builds the context. It works with plain Spring (@ContextConfiguration) and with Spring Boot (@SpringBootTest, @DataJpaTest, @WebMvcTest).
Spring resolves @Value, @ConfigurationProperties and Environment.getProperty() calls from an ordered list of property sources. @TestPropertySource inserts a source near the top of that list for the duration of the test. Anything defined in the test source wins over:
application.properties or application.yml@PropertySource on configuration classesIt is part of spring-test, in the package org.springframework.test.context. Because the overrides are scoped to the test class, other test classes keep seeing the normal configuration.
The annotation has two main attributes: locations (or its alias value) for property files, and properties for inline entries.
The file src/test/resources/test-overrides.properties could contain:
Inline properties entries take precedence over entries loaded from locations, so app.mail.sender above cannot be overridden by the file.
Each inline entry can use key=value, key:value or key value. Since Spring Framework 5.2 you can also write a text block or a multi-line string, which is convenient on Java 17 and later:
If you declare @TestPropertySource with no attributes, Spring looks for a file named after the test class in the same package. For com.example.OrderServiceTest that is classpath:com/example/OrderServiceTest.properties. Spring throws an IllegalStateException if the default file is missing, so only use the empty form when you really have that file.
Property order matters when several mechanisms set the same key. From highest to lowest among the test-specific ones:
| Mechanism | Scope | Notes |
|---|---|---|
@DynamicPropertySource | Test class, method computed at runtime | Beats @TestPropertySource; ideal for Testcontainers ports |
@TestPropertySource inline properties | Test class | Highest static override |
@TestPropertySource locations | Test class | Files, lower than inline entries |
@SpringBootTest(properties = ...) | Test class | Spring Boot only; lower than @TestPropertySource |
application.properties and the rest | Whole application | Normal configuration |
@TestPropertySource is not the right tool when a value is only known at runtime, such as a random container port. Use @DynamicPropertySource for that, and keep @TestPropertySource for fixed values.
Two attributes control inheritance from superclasses: inheritLocations and inheritProperties, both true by default. When they are true, a subclass sees the files and inline entries of its parent, and its own entries override them. Set either to false to start clean.
Since Spring Framework 5.2 the annotation is repeatable, so you can declare it several times on one class or in a meta-annotation.
Spring caches an ApplicationContext and reuses it across test classes that have an identical configuration. The property sources are part of the cache key. Two test classes with different @TestPropertySource settings therefore get two separate contexts, which is correct but slower. If your suite starts many contexts, group tests that share the same overrides, and avoid adding a unique property to every class.
@PropertySource in a test and expecting it to override application.properties. It has lower precedence than the environment and cannot override those values reliably.@TestPropertySource on a method. It only works on classes.classpath: prefix. A bare path like test.properties is relative to the test class package.locations loads properties files only. For YAML, use application-test.yml with @ActiveProfiles("test"), or inline properties.Environment in one test and expecting a clean state in the next. Use @DirtiesContext only when you must, because it discards the cached context.@PropertySource is a production feature that adds a file to the Environment from a configuration class. @TestPropertySource is a test feature with higher precedence, so it can override values from @PropertySource, the environment and application.properties.
Yes. Profiles select which application-{profile}.properties files load, and @TestPropertySource then overrides individual keys on top of that result.
Both set properties for a test, but @TestPropertySource has higher precedence. Use @SpringBootTest(properties = ...) for a quick override and @TestPropertySource when you need files, inheritance control or repeatable declarations.
Yes, as it belongs to the Spring TestContext Framework. With JUnit 5, @SpringBootTest already registers the Spring extension. With JUnit 4 you need SpringRunner or SpringClassRule.
@TestPropertySource lets a test class replace configuration values through property files (locations) or inline entries (properties), with precedence above the environment and application configuration. Only @DynamicPropertySource is higher. Remember that different settings create separate cached contexts, so keep overrides consistent across related tests.