注释类型 TestPropertySource
@Target(TYPE) @Retention(RUNTIME) @Documented @Inherited @Repeatable(TestPropertySources.class) public @interface TestPropertySource
@TestPropertySourceis a class-level annotation that is used to configure thelocations()of properties files and inlinedproperties()to be added to theEnvironment's set ofPropertySourcesfor anApplicationContextfor integration tests.Precedence
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
@PropertySourceor programmatically (e.g., via anApplicationContextInitializeror some other means). Thus, test property sources can be used to selectively override properties defined in system and application property sources. Furthermore, inlinedproperties()have higher precedence than properties loaded from resourcelocations(). Note, however, that properties registered via@DynamicPropertySourcehave higher precedence than those loaded via@TestPropertySource.Default Properties File Detection
If
@TestPropertySourceis declared as an empty annotation (i.e., without explicit values forlocations()orproperties()), 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 iscom.example.MyTest, the corresponding default properties file is"classpath:com/example/MyTest.properties". If the default cannot be detected, anIllegalStateExceptionwill be thrown.Enabling @TestPropertySource
@TestPropertySourceis enabled if the configured context loader honors it. EverySmartContextLoaderthat is a subclass of eitherAbstractGenericContextLoaderorAbstractGenericWebContextLoaderprovides automatic support for@TestPropertySource, and this includes everySmartContextLoaderprovided by the Spring TestContext Framework.Miscellaneous
- Typically,
@TestPropertySourcewill be used in conjunction with@ContextConfiguration. - As of Spring Framework 5.2,
@TestPropertySourcecan be used as a repeatable annotation. - This annotation may be used as a meta-annotation to create custom composed annotations; however, caution should be taken if this annotation and
@ContextConfigurationare combined on a composed annotation since thelocationsandinheritLocationsattributes of both annotations can lead to ambiguity during the attribute resolution process.
- 从以下版本开始:
- 4.1
- 作者:
- Sam Brannen
- 另请参阅:
ContextConfiguration,DynamicPropertySource,Environment,PropertySource,PropertySource
- Typically,
可选元素概要
可选元素 修饰符和类型 可选元素 说明 booleaninheritLocationsWhether or not test property sourcelocations()from superclasses should be inherited.booleaninheritPropertiesWhether or not inlined testproperties()from superclasses should be inherited.String[]locationsThe resource locations of properties files to be loaded into theEnvironment's set ofPropertySources.String[]propertiesInlined properties in the form of key-value pairs that should be added to the SpringEnvironmentbefore theApplicationContextis loaded for the test.String[]valueAlias forlocations().
元素详细资料
value
@AliasFor("locations") String[] value
Alias forlocations().This attribute may not be used in conjunction with
locations(), but it may be used instead oflocations().- 另请参阅:
locations()
- 默认值:
- {}
locations
@AliasFor("value") String[] locations
The resource locations of properties files to be loaded into theEnvironment's set ofPropertySources. Each location will be added to the enclosingEnvironmentas its own property source, in the order declared.Supported File Formats
Both traditional and XML-based properties file formats are supported — for example,
"classpath:/com/example/test.properties"or"file:/path/to/file.xml".Path Resource Semantics
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 withclasspath:,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.propertiesor.xmlresource. Property placeholders in paths (i.e.,${...}) will be resolved against theEnvironment.Default Properties File Detection
See the class-level Javadoc for a discussion on detection of defaults.
Precedence
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 ofvalue().- 默认值:
- {}
inheritLocations
boolean inheritLocations
Whether or not test property sourcelocations()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
inheritLocationsis set tofalse, 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
ApplicationContextforBaseTestwill be loaded using only the"base.properties"file as a test property source. In contrast, theApplicationContextforExtendedTestwill 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 { // ... }If
@TestPropertySourceis used as a repeatable annotation, the following special rules apply.- All
@TestPropertySourceannotations at a given level in the test class hierarchy (i.e., directly present or meta-present on a test class) are considered to be local annotations, in contrast to@TestPropertySourceannotations that are inherited from a superclass. - All local
@TestPropertySourceannotations must declare the same value for theinheritLocationsflag. - The
inheritLocationsflag is not taken into account between local@TestPropertySourceannotations. Specifically, the property source locations for one local annotation will be appended to the list of property source locations defined by previous local annotations. This allows a local annotation to extend the list of test property source locations, potentially overriding individual properties.
- 另请参阅:
locations()
- 默认值:
- true
- All
properties
String[] properties
Inlined properties in the form of key-value pairs that should be added to the SpringEnvironmentbefore theApplicationContextis loaded for the test. All key-value pairs will be added to the enclosingEnvironmentas a single testPropertySourcewith the highest precedence.Supported Syntax
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"
Precedence
Properties declared via this attribute have higher precedence than properties loaded from resource
locations().This attribute may be used in conjunction with
value()orlocations().- 默认值:
- {}
inheritProperties
boolean inheritProperties
Whether or not inlined testproperties()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
inheritPropertiesis set tofalse, the inlined properties for the test class will shadow and effectively replace any inlined properties defined by a superclass.In the following example, the
ApplicationContextforBaseTestwill be loaded using only the inlinedkey1property. In contrast, theApplicationContextforExtendedTestwill be loaded using the inlinedkey1andkey2properties.@TestPropertySource(properties = "key1 = value1") @ContextConfiguration public class BaseTest { // ... } @TestPropertySource(properties = "key2 = value2") @ContextConfiguration public class ExtendedTest extends BaseTest { // ... }If
@TestPropertySourceis used as a repeatable annotation, the following special rules apply.- All
@TestPropertySourceannotations at a given level in the test class hierarchy (i.e., directly present or meta-present on a test class) are considered to be local annotations, in contrast to@TestPropertySourceannotations that are inherited from a superclass. - All local
@TestPropertySourceannotations must declare the same value for theinheritPropertiesflag. - The
inheritPropertiesflag is not taken into account between local@TestPropertySourceannotations. Specifically, inlined properties for one local annotation will be appended to the list of inlined properties defined by previous local annotations. This allows a local annotation to extend the list of inlined properties, potentially overriding individual properties.
- 另请参阅:
properties()
- 默认值:
- true
- All