Skip to content

Commit 1be29d5

Browse files
committed
Document automatic constructor injection in JUnit Jupiter
Closes gh-22928
1 parent c601ecf commit 1be29d5

File tree

1 file changed

+67
-6
lines changed

1 file changed

+67
-6
lines changed

src/docs/asciidoc/testing.adoc

+67-6
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ The following annotations are supported only when used in conjunction with the
11891189

11901190
* <<integration-testing-annotations-junit-jupiter-springjunitconfig>>
11911191
* <<integration-testing-annotations-junit-jupiter-springjunitwebconfig>>
1192+
* <<integration-testing-annotations-testconstructor>>
11921193
* <<integration-testing-annotations-junit-jupiter-enabledif>>
11931194
* <<integration-testing-annotations-junit-jupiter-disabledif>>
11941195

@@ -1281,6 +1282,35 @@ See <<testcontext-ctx-management>> as well as the javadoc for
12811282
{api-spring-framework}/test/context/web/WebAppConfiguration.html[`@WebAppConfiguration`]
12821283
for further details.
12831284

1285+
[[integration-testing-annotations-testconstructor]]
1286+
===== `@TestConstructor`
1287+
1288+
`@TestConstructor` is a type-level annotation that is used to configure whether a test
1289+
class constructor should be automatically autowired from components in the test's
1290+
`ApplicationContext`.
1291+
1292+
If `@TestConstructor` is not present or meta-present on a test class, the default _test
1293+
constructor autowire_ mode will be used. See the tip below for details on how to change
1294+
the default mode. Note, however, that a local declaration of `@Autowired` on a
1295+
constructor takes precedence over both `@TestConstructor` and the default mode.
1296+
1297+
.Configuring the default test constructor autowire mode
1298+
[TIP]
1299+
=====
1300+
The default _test constructor autowire_ mode can be configured by setting the
1301+
`spring.test.constructor.autowire` JVM system property to `true`. Alternatively, the
1302+
default mode may be configured via the `SpringProperties` mechanism.
1303+
1304+
If the `spring.test.constructor.autowire` property is not set, test class constructors
1305+
will not be automatically autowired.
1306+
=====
1307+
1308+
NOTE: As of Spring Framework 5.2, `@TestConstructor` is only supported in conjunction
1309+
with the `SpringExtension` for use with JUnit Jupiter. Note that the `SpringExtension` is
1310+
often automatically registered for you – for example, when using annotations such as
1311+
`@SpringJUnitConfig` and `@SpringJUnitWebConfig` or various test-related annotations from
1312+
Spring Boot Test.
1313+
12841314
[[integration-testing-annotations-junit-jupiter-enabledif]]
12851315
===== `@EnabledIf`
12861316

@@ -1386,6 +1416,7 @@ You can use each of the following as a meta-annotation in conjunction with the
13861416
* `@ProfileValueSourceConfiguration` _(only supported on JUnit 4)_
13871417
* `@SpringJUnitConfig` _(only supported on JUnit Jupiter)_
13881418
* `@SpringJUnitWebConfig` _(only supported on JUnit Jupiter)_
1419+
* `@TestConstructor` _(only supported on JUnit Jupiter)_
13891420
* `@EnabledIf` _(only supported on JUnit Jupiter)_
13901421
* `@DisabledIf` _(only supported on JUnit Jupiter)_
13911422

@@ -4423,15 +4454,25 @@ Specifically, `SpringExtension` can inject dependencies from the test's
44234454
[[testcontext-junit-jupiter-di-constructor]]
44244455
====== Constructor Injection
44254456

4426-
If a parameter in a constructor for a JUnit Jupiter test class is of type
4457+
If a specific parameter in a constructor for a JUnit Jupiter test class is of type
44274458
`ApplicationContext` (or a sub-type thereof) or is annotated or meta-annotated with
44284459
`@Autowired`, `@Qualifier`, or `@Value`, Spring injects the value for that specific
4429-
parameter with the corresponding bean from the test's `ApplicationContext`. You can also
4430-
directly annotate a test constructor with `@Autowired` if all of the parameters should be
4431-
supplied by Spring.
4460+
parameter with the corresponding bean or value from the test's `ApplicationContext`.
4461+
4462+
Spring can also be configured to autowire all arguments for a test class constructor if
4463+
the constructor is considered to be _autowirable_. A constructor is considered to be
4464+
autowirable if one of the following conditions is met (in order of precedence).
4465+
4466+
* The constructor is annotated with `@Autowired`.
4467+
* `@TestConstructor` is present or meta-present on the test class with the `autowire`
4468+
attribute set to `true`.
4469+
* The default _test constructor autowire_ mode is set to `true`.
4470+
4471+
See <<integration-testing-annotations-testconstructor>> for details on the use of
4472+
`@TestConstructor` and how to set the global _test constructor autowire_ mode.
44324473

4433-
WARNING: If the constructor for a test class is itself annotated with `@Autowired`,
4434-
Spring assumes the responsibility for resolving _all_ parameters in the constructor.
4474+
WARNING: If the constructor for a test class is considered to be _autowirable_, Spring
4475+
assumes the responsibility for resolving arguments for all parameters in the constructor.
44354476
Consequently, no other `ParameterResolver` registered with JUnit Jupiter can resolve
44364477
parameters for such a constructor.
44374478

@@ -4477,6 +4518,26 @@ In the following example, Spring injects the `OrderService` bean from the
44774518

44784519
Note that this feature lets test dependencies be `final` and therefore immutable.
44794520

4521+
If the `spring.test.constructor.autowire` property is to `true` (see
4522+
<<integration-testing-annotations-testconstructor>>), we can omit the declaration of
4523+
`@Autowired` on the constructor in the previous example resulting in the following.
4524+
4525+
[source,java,indent=0]
4526+
[subs="verbatim,quotes"]
4527+
----
4528+
@SpringJUnitConfig(TestConfig.class)
4529+
class OrderServiceIntegrationTests {
4530+
4531+
private final OrderService orderService;
4532+
4533+
OrderServiceIntegrationTests(OrderService orderService) {
4534+
this.orderService = orderService.
4535+
}
4536+
4537+
// tests that use the injected OrderService
4538+
}
4539+
----
4540+
44804541
[[testcontext-junit-jupiter-di-method]]
44814542
====== Method Injection
44824543

0 commit comments

Comments
 (0)