@Documented @Inherited @Retention(value=RUNTIME) @Target(value=TYPE) public @interface TestPropertySource
@TestPropertySource
is a class-level annotation that is used to
configure the locations()
of properties files and inlined
properties()
to be added to the Environment
's set of
PropertySources
for an
ApplicationContext
for integration tests.
Test property sources have higher precedence than those loaded from the
operating system's environment or Java system properties as well as property
sources added by the application declaratively via
@PropertySource
or programmatically (e.g., via an
ApplicationContextInitializer
or some other means). Thus, test property sources can be used to selectively
override properties defined in system and application property sources.
Furthermore, inlined properties()
have higher precedence than
properties loaded from resource locations()
.
If @TestPropertySource
is declared as an empty annotation
(i.e., without explicit values for locations()
or properties()
),
an attempt will be made to detect a default properties file relative
to the class that declared the annotation. For example, if the annotated test
class is com.example.MyTest
, the corresponding default properties file
is "classpath:com/example/MyTest.properties"
. If the default cannot be
detected, an IllegalStateException
will be thrown.
@TestPropertySource
is enabled if the configured
context loader honors it. Every
SmartContextLoader
that is a subclass of either
AbstractGenericContextLoader
or
AbstractGenericWebContextLoader
provides automatic support for @TestPropertySource
, and this includes
every SmartContextLoader
provided by the Spring TestContext Framework.
@TestPropertySource
will be used in conjunction with
@ContextConfiguration
.@ContextConfiguration
are combined on a composed
annotation since the locations
and inheritLocations
attributes
of both annotations can lead to ambiguity during the attribute resolution
process.ContextConfiguration
,
Environment
,
PropertySource
,
PropertySource
Modifier and Type | Optional Element and Description |
---|---|
boolean |
inheritLocations
Whether or not test property source
locations() from superclasses
should be inherited. |
boolean |
inheritProperties
Whether or not inlined test
properties() from superclasses should
be inherited. |
String[] |
locations
The resource locations of properties files to be loaded into the
Environment 's set of PropertySources . |
String[] |
properties
Inlined properties in the form of key-value pairs that
should be added to the Spring
Environment before the
ApplicationContext is loaded for the test. |
String[] |
value
Alias for
locations() . |
public abstract String[] value
locations()
.
This attribute may not be used in conjunction with
locations()
, but it may be used instead of locations()
.
locations()
public abstract String[] locations
Environment
's set of PropertySources
. Each location
will be added to the enclosing Environment
as its own property
source, in the order declared.
Both traditional and XML-based properties file formats are supported
— for example, "classpath:/com/example/test.properties"
or "file:/path/to/file.xml"
.
Each path will be interpreted as a Spring
Resource
. A plain path
— for example, "test.properties"
— will be treated as a
classpath resource that is relative to the package in which the
test class is defined. A path starting with a slash will be treated as an
absolute classpath resource, for example:
"/org/example/test.xml"
. A path which references a
URL (e.g., a path prefixed with
classpath:
,
file:
,
http:
, etc.) will be loaded using the specified resource protocol.
Resource location wildcards (e.g. **/*.properties
)
are not permitted: each location must evaluate to exactly one
.properties
or .xml
resource. Property placeholders
in paths (i.e., ${...}
) will be
resolved
against the Environment
.
See the class-level Javadoc for a discussion on detection of defaults.
Properties loaded from resource locations have lower precedence than
inlined properties()
.
This attribute may not be used in conjunction with
value()
, but it may be used instead of value()
.
inheritLocations()
,
value()
,
properties()
,
PropertySource
public abstract boolean inheritLocations
locations()
from superclasses
should be inherited.
The default value is true
, which means that a test class will
inherit property source locations defined by a superclass.
Specifically, the property source locations for a test class will be
appended to the list of property source locations defined by a superclass.
Thus, subclasses have the option of extending the list of test
property source locations.
If inheritLocations
is set to false
, the property
source locations for the test class will shadow and effectively
replace any property source locations defined by a superclass.
In the following example, the ApplicationContext
for
BaseTest
will be loaded using only the "base.properties"
file as a test property source. In contrast, the ApplicationContext
for ExtendedTest
will be loaded using the "base.properties"
and "extended.properties"
files as test property
source locations.
@TestPropertySource("base.properties") @ContextConfiguration public class BaseTest { // ... } @TestPropertySource("extended.properties") @ContextConfiguration public class ExtendedTest extends BaseTest { // ... }
locations()
public abstract String[] properties
Environment
before the
ApplicationContext
is loaded for the test. All key-value pairs
will be added to the enclosing Environment
as a single test
PropertySource
with the highest precedence.
The supported syntax for key-value pairs is the same as the syntax defined for entries in a Java properties file:
"key=value"
"key:value"
"key value"
Properties declared via this attribute have higher precedence than
properties loaded from resource locations()
.
This attribute may be used in conjunction with value()
or locations()
.
inheritProperties()
,
locations()
,
PropertySource
public abstract boolean inheritProperties
properties()
from superclasses should
be inherited.
The default value is true
, which means that a test class will
inherit inlined properties defined by a superclass. Specifically,
the inlined properties for a test class will be appended to the list of
inlined properties defined by a superclass. Thus, subclasses have the
option of extending the list of inlined test properties.
If inheritProperties
is set to false
, the inlined
properties for the test class will shadow and effectively
replace any inlined properties defined by a superclass.
In the following example, the ApplicationContext
for
BaseTest
will be loaded using only the inlined key1
property. In contrast, the ApplicationContext
for
ExtendedTest
will be loaded using the inlined key1
and key2
properties.
@TestPropertySource(properties = "key1 = value1") @ContextConfiguration public class BaseTest { // ... } @TestPropertySource(properties = "key2 = value2") @ContextConfiguration public class ExtendedTest extends BaseTest { // ... }
properties()