Spring Framework 4.3 introduced the `PropertySourceFactory` SPI for use
with `@PropertySource` on `@Configuration` classes; however, prior to
this commit there was no mechanism to support custom properties file
formats in `@TestPropertySource` for integration tests.
This commit introduces support for configuring a custom
`PropertySourceFactory` via a new `factory` attribute in
`@TestPropertySource` in order to support custom file formats such as
JSON, YAML, etc.
For example, if you create a YamlPropertySourceFactory, you can use it
in integration tests as follows.
@SpringJUnitConfig
@TestPropertySource(locations = "/test.yaml", factory = YamlPropertySourceFactory.class)
class MyTestClass { /* ... /* }
If a custom factory is not specified, traditional `*.properties` and
`*.xml` based `java.util.Properties` file formats are supported, which
was the existing behavior.
Closes gh-30981
@ -16,7 +16,7 @@ SPI, but `@TestPropertySource` is not supported with implementations of the olde
@@ -16,7 +16,7 @@ SPI, but `@TestPropertySource` is not supported with implementations of the olde
`ContextLoader` SPI.
Implementations of `SmartContextLoader` gain access to merged test property source values
through the `getPropertySourceLocations()` and `getPropertySourceProperties()` methods in
through the `getPropertySourceDescriptors()` and `getPropertySourceProperties()` methods in
`MergedContextConfiguration`.
====
@ -26,8 +26,11 @@ through the `getPropertySourceLocations()` and `getPropertySourceProperties()` m
@@ -26,8 +26,11 @@ through the `getPropertySourceLocations()` and `getPropertySourceProperties()` m
You can configure test properties files by using the `locations` or `value` attribute of
`@TestPropertySource`.
Both traditional and XML-based properties file formats are supported -- for example,
`"classpath:/com/example/test.properties"` or `"file:///path/to/file.xml"`.
By default, both traditional and XML-based `java.util.Properties` file formats are
supported -- for example, `"classpath:/com/example/test.properties"` or
`"file:///path/to/file.xml"`. As of Spring Framework 6.1, you can configure a custom
`PropertySourceFactory` via the `factory` attribute in `@TestPropertySource` in order to
support a different file format such as JSON, YAML, etc.
Each path is interpreted as a Spring `Resource`. A plain path (for example,
`"test.properties"`) is treated as a classpath resource that is relative to the package
@ -35,8 +38,8 @@ in which the test class is defined. A path starting with a slash is treated as a
@@ -35,8 +38,8 @@ in which the test class is defined. A path starting with a slash is treated as a
absolute classpath resource (for example: `"/org/example/test.xml"`). A path that
references a URL (for example, a path prefixed with `classpath:`, `file:`, or `http:`) is
loaded by using the specified resource protocol. Resource location wildcards (such as
`**/*.properties`) are not permitted: Each location must evaluate to exactly one
`.properties` or `.xml` resource.
`{asterisk}{asterisk}/{asterisk}.properties`) are not permitted: Each location must
evaluate to exactly one properties resource.
The following example uses a test properties file:
@ -91,6 +93,8 @@ public class MergedContextConfiguration implements Serializable {
@@ -91,6 +93,8 @@ public class MergedContextConfiguration implements Serializable {
@ -151,7 +155,7 @@ public class MergedContextConfiguration implements Serializable {
@@ -151,7 +155,7 @@ public class MergedContextConfiguration implements Serializable {
@ -172,9 +176,10 @@ public class MergedContextConfiguration implements Serializable {
@@ -172,9 +176,10 @@ public class MergedContextConfiguration implements Serializable {
@ -196,10 +201,13 @@ public class MergedContextConfiguration implements Serializable {
@@ -196,10 +201,13 @@ public class MergedContextConfiguration implements Serializable {
@ -233,10 +241,13 @@ public class MergedContextConfiguration implements Serializable {
@@ -233,10 +241,13 @@ public class MergedContextConfiguration implements Serializable {
@ -244,12 +255,52 @@ public class MergedContextConfiguration implements Serializable {
@@ -244,12 +255,52 @@ public class MergedContextConfiguration implements Serializable {
@ -338,18 +389,32 @@ public class MergedContextConfiguration implements Serializable {
@@ -338,18 +389,32 @@ public class MergedContextConfiguration implements Serializable {
@ -408,12 +473,13 @@ public class MergedContextConfiguration implements Serializable {
@@ -408,12 +473,13 @@ public class MergedContextConfiguration implements Serializable {
@ -439,7 +505,7 @@ public class MergedContextConfiguration implements Serializable {
@@ -439,7 +505,7 @@ public class MergedContextConfiguration implements Serializable {
@ -476,7 +542,7 @@ public class MergedContextConfiguration implements Serializable {
@@ -476,7 +542,7 @@ public class MergedContextConfiguration implements Serializable {
@ -489,7 +555,7 @@ public class MergedContextConfiguration implements Serializable {
@@ -489,7 +555,7 @@ public class MergedContextConfiguration implements Serializable {
@ -503,7 +569,7 @@ public class MergedContextConfiguration implements Serializable {
@@ -503,7 +569,7 @@ public class MergedContextConfiguration implements Serializable {
@ -512,7 +578,7 @@ public class MergedContextConfiguration implements Serializable {
@@ -512,7 +578,7 @@ public class MergedContextConfiguration implements Serializable {
@ -368,7 +368,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
@@ -368,7 +368,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
@ -99,7 +101,10 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@@ -99,7 +101,10 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@ -135,14 +140,54 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@@ -135,14 +140,54 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@ -160,11 +205,14 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@@ -160,11 +205,14 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@ -189,7 +237,7 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@@ -189,7 +237,7 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@ -204,7 +252,7 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
@@ -204,7 +252,7 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {