Skip to content

Commit 279e6eb

Browse files
committed
Document best practices for inlined properties in @TestPropertySource
1 parent eb65939 commit 279e6eb

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management/property-sources.adoc

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ a Java properties file:
8686
* `key:value`
8787
* `key value`
8888

89+
[TIP]
90+
====
91+
Although properties can be defined using any of the above syntax variants and any number
92+
of spaces between the key and the value, it is recommended that you use one syntax
93+
variant and consistent spacing within your test suite — for example, consider always
94+
using `key = value` instead of `key= value`, `key=value`, etc. Similarly, if you define
95+
inlined properties using text blocks you should consistently use text blocks for inlined
96+
properties throughout your test suite.
97+
98+
The reason is that the exact strings you provide will be used to determine the key for
99+
the context cache. Consequently, to benefit from the context cache you must ensure that
100+
you define inlined properties consistently.
101+
====
102+
89103
The following example sets two inlined properties:
90104

91105
[tabs]
@@ -95,24 +109,61 @@ Java::
95109
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
96110
----
97111
@ContextConfiguration
98-
@TestPropertySource(properties = {"timezone = GMT", "port: 4242"}) // <1>
112+
@TestPropertySource(properties = {"timezone = GMT", "port = 4242"}) // <1>
113+
class MyIntegrationTests {
114+
// class body...
115+
}
116+
----
117+
<1> Setting two properties via an array of strings.
118+
119+
Kotlin::
120+
+
121+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
122+
----
123+
@ContextConfiguration
124+
@TestPropertySource(properties = ["timezone = GMT", "port = 4242"]) // <1>
125+
class MyIntegrationTests {
126+
// class body...
127+
}
128+
----
129+
<1> Setting two properties via an array of strings.
130+
======
131+
132+
As of Spring Framework 6.1, you can use _text blocks_ to define multiple inlined
133+
properties in a single `String`. The following example sets two inlined properties using
134+
a text block:
135+
136+
[tabs]
137+
======
138+
Java::
139+
+
140+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
141+
----
142+
@ContextConfiguration
143+
@TestPropertySource(properties = """
144+
timezone = GMT
145+
port = 4242
146+
""") // <1>
99147
class MyIntegrationTests {
100148
// class body...
101149
}
102150
----
103-
<1> Setting two properties by using two variations of the key-value syntax.
151+
<1> Setting two properties via a text block.
104152
105153
Kotlin::
106154
+
107155
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
108156
----
109157
@ContextConfiguration
110-
@TestPropertySource(properties = ["timezone = GMT", "port: 4242"]) // <1>
158+
@TestPropertySource(properties = ["""
159+
timezone = GMT
160+
port = 4242
161+
"""]) // <1>
111162
class MyIntegrationTests {
112163
// class body...
113164
}
114165
----
115-
<1> Setting two properties by using two variations of the key-value syntax.
166+
<1> Setting two properties via a text block.
116167
======
117168

118169
[NOTE]
@@ -172,7 +223,7 @@ Java::
172223
@ContextConfiguration
173224
@TestPropertySource(
174225
locations = "/test.properties",
175-
properties = {"timezone = GMT", "port: 4242"}
226+
properties = {"timezone = GMT", "port = 4242"}
176227
)
177228
class MyIntegrationTests {
178229
// class body...
@@ -185,7 +236,7 @@ Kotlin::
185236
----
186237
@ContextConfiguration
187238
@TestPropertySource("/test.properties",
188-
properties = ["timezone = GMT", "port: 4242"]
239+
properties = ["timezone = GMT", "port = 4242"]
189240
)
190241
class MyIntegrationTests {
191242
// class body...

spring-test/src/main/java/org/springframework/test/context/TestPropertySource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@
231231
* <li>{@code "key:value"}</li>
232232
* <li>{@code "key value"}</li>
233233
* </ul>
234+
* <p><strong>WARNING</strong>: although properties can be defined using any
235+
* of the above syntax variants and any number of spaces between the key and
236+
* the value, it is recommended that you use one syntax variant and consistent
237+
* spacing within your test suite &mdash; for example, consider always using
238+
* {@code "key = value"} instead of {@code "key= value"}, {@code "key=value"},
239+
* etc. Similarly, if you define inlined properties using <em>text blocks</em>
240+
* you should consistently use text blocks for inlined properties throughout
241+
* your test suite. The reason is that the exact strings you provide will be
242+
* used to determine the key for the context cache. Consequently, to benefit
243+
* from the context cache you must ensure that you define inlined properties
244+
* consistently.
234245
* <h4>Examples</h4>
235246
* <pre class="code">
236247
* &#47;&#47; Using an array of strings

0 commit comments

Comments
 (0)