Skip to content

Commit a08a4d6

Browse files
committed
Document @⁠SpringExtensionConfig in the reference manual
This commit also updates the Javadoc for the SpringExtension and @⁠SpringExtensionConfig to point out that the SpringExtension always uses a test-class scoped ExtensionContext if @⁠TestInstance(Lifecycle.PER_CLASS) semantics are in effect. Closes gh-36240
1 parent 6fd84e4 commit a08a4d6

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

framework-docs/modules/ROOT/pages/testing/annotations/integration-junit-jupiter.adoc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The following annotations are supported when used in conjunction with the
55
xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-extension[`SpringExtension`]
66
and JUnit Jupiter (that is, the programming model in JUnit):
77

8+
* xref:testing/annotations/integration-junit-jupiter.adoc#integration-testing-annotations-springextensionconfig[`@SpringExtensionConfig`]
89
* xref:testing/annotations/integration-junit-jupiter.adoc#integration-testing-annotations-junit-jupiter-springjunitconfig[`@SpringJUnitConfig`]
910
* xref:testing/annotations/integration-junit-jupiter.adoc#integration-testing-annotations-junit-jupiter-springjunitwebconfig[`@SpringJUnitWebConfig`]
1011
* xref:testing/annotations/integration-junit-jupiter.adoc#integration-testing-annotations-testconstructor[`@TestConstructor`]
@@ -14,6 +15,47 @@ and JUnit Jupiter (that is, the programming model in JUnit):
1415
* xref:testing/annotations/integration-spring/annotation-disabledinaotmode.adoc[`@DisabledInAotMode`]
1516

1617

18+
[[integration-testing-annotations-springextensionconfig]]
19+
== `@SpringExtensionConfig`
20+
21+
`@SpringExtensionConfig` is a type-level annotation that can be used to configure the
22+
behavior of the `SpringExtension`.
23+
24+
As of Spring Framework 7.0, the `SpringExtension` is configured to use a test-method
25+
scoped `ExtensionContext`, which enables consistent dependency injection into fields and
26+
constructors from the `ApplicationContext` for the current test method in a `@Nested`
27+
test class hierarchy. However, if a third-party `TestExecutionListener` is not compatible
28+
with the semantics associated with a test-method scoped extension context — or if a
29+
developer wishes to switch to test-class scoped semantics — the `SpringExtension` can be
30+
configured to use a test-class scoped `ExtensionContext` by annotating a top-level test
31+
class with `@SpringExtensionConfig(useTestClassScopedExtensionContext = true)`.
32+
33+
[TIP]
34+
====
35+
If your top-level test class is configured to use JUnit Jupiter's
36+
`@TestInstance(Lifecycle.PER_CLASS)` semantics, the `SpringExtension` will always use a
37+
test-class scoped `ExtensionContext`, and there is no need to declare
38+
`@SpringExtensionConfig(useTestClassScopedExtensionContext = true)`.
39+
====
40+
41+
[NOTE]
42+
====
43+
This annotation is currently only applicable to `@Nested` test class hierarchies and
44+
should be applied to the top-level enclosing class of a `@Nested` test class hierarchy.
45+
46+
Consequently, there is no need to declare this annotation on a test class that does not
47+
contain `@Nested` test classes.
48+
====
49+
50+
[NOTE]
51+
====
52+
xref:testing/annotations/integration-junit-jupiter.adoc#integration-testing-annotations-nestedtestconfiguration[`@NestedTestConfiguration`]
53+
does not apply to this annotation.
54+
55+
`@SpringExtensionConfig` will always be detected within a `@Nested` test class hierarchy,
56+
effectively disregarding any `@NestedTestConfiguration(OVERRIDE)` declarations.
57+
====
58+
1759
[[integration-testing-annotations-junit-jupiter-springjunitconfig]]
1860
== `@SpringJUnitConfig`
1961

framework-docs/modules/ROOT/pages/testing/testcontext-framework/support-classes.adoc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,17 @@ any of its subclasses and nested classes. Thus, you may annotate a top-level tes
392392
with `@NestedTestConfiguration`, and that will apply to all of its nested test classes
393393
recursively.
394394

395+
[NOTE]
396+
====
397+
As of Spring Framework 7.0, the `SpringExtension` uses a test-method scoped
398+
`ExtensionContext` within `@Nested` test class hierarchies by default. However, the
399+
`SpringExtension` can be configured to use a test-class scoped `ExtensionContext`.
400+
401+
See the documentation for
402+
xref:testing/annotations/integration-junit-jupiter.adoc#integration-testing-annotations-springextensionconfig[`@SpringExtensionConfig`]
403+
for details.
404+
====
405+
395406
[TIP]
396407
====
397408
If you are developing a component that integrates with the Spring TestContext Framework

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@
8888
* be configured to use a test-class scoped extension context by annotating a
8989
* top-level test class with
9090
* {@link SpringExtensionConfig#useTestClassScopedExtensionContext()
91-
* @SpringExtensionConfig(useTestClassScopedExtensionContext = true)}.
91+
* @SpringExtensionConfig(useTestClassScopedExtensionContext = true)}. Note,
92+
* however, that the {@code SpringExtension} will always use a test-class scoped
93+
* {@code ExtensionContext} if your top-level test class is configured to use JUnit
94+
* Jupiter’s {@code @TestInstance(Lifecycle.PER_CLASS)} semantics, in which case
95+
* there is no need to declare
96+
* {@code @SpringExtensionConfig(useTestClassScopedExtensionContext = true)}.
9297
*
9398
* <p><strong>NOTE:</strong> This class requires JUnit Jupiter 6.0 or higher.
9499
*

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtensionConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
* {@code ExtensionContext}. Thus, there is no need to declare this annotation
6161
* attribute with a value of {@code false}.
6262
*
63+
* <p>Similarly, if your top-level test class is configured to use JUnit Jupiter’s
64+
* {@code @TestInstance(Lifecycle.PER_CLASS)} semantics, the {@code SpringExtension}
65+
* will always use a test-class scoped {@code ExtensionContext}, and there is no need
66+
* to declare {@code @SpringExtensionConfig(useTestClassScopedExtensionContext = true)}.
67+
*
6368
* @see SpringExtension
6469
* @see SpringExtension#getTestInstantiationExtensionContextScope(org.junit.jupiter.api.extension.ExtensionContext)
6570
*/

0 commit comments

Comments
 (0)