@@ -1189,6 +1189,7 @@ The following annotations are supported only when used in conjunction with the
1189
1189
1190
1190
* <<integration-testing-annotations-junit-jupiter-springjunitconfig>>
1191
1191
* <<integration-testing-annotations-junit-jupiter-springjunitwebconfig>>
1192
+ * <<integration-testing-annotations-testconstructor>>
1192
1193
* <<integration-testing-annotations-junit-jupiter-enabledif>>
1193
1194
* <<integration-testing-annotations-junit-jupiter-disabledif>>
1194
1195
@@ -1281,6 +1282,35 @@ See <<testcontext-ctx-management>> as well as the javadoc for
1281
1282
{api-spring-framework}/test/context/web/WebAppConfiguration.html[`@WebAppConfiguration`]
1282
1283
for further details.
1283
1284
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
+
1284
1314
[[integration-testing-annotations-junit-jupiter-enabledif]]
1285
1315
===== `@EnabledIf`
1286
1316
@@ -1386,6 +1416,7 @@ You can use each of the following as a meta-annotation in conjunction with the
1386
1416
* `@ProfileValueSourceConfiguration` _(only supported on JUnit 4)_
1387
1417
* `@SpringJUnitConfig` _(only supported on JUnit Jupiter)_
1388
1418
* `@SpringJUnitWebConfig` _(only supported on JUnit Jupiter)_
1419
+ * `@TestConstructor` _(only supported on JUnit Jupiter)_
1389
1420
* `@EnabledIf` _(only supported on JUnit Jupiter)_
1390
1421
* `@DisabledIf` _(only supported on JUnit Jupiter)_
1391
1422
@@ -4423,15 +4454,25 @@ Specifically, `SpringExtension` can inject dependencies from the test's
4423
4454
[[testcontext-junit-jupiter-di-constructor]]
4424
4455
====== Constructor Injection
4425
4456
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
4427
4458
`ApplicationContext` (or a sub-type thereof) or is annotated or meta-annotated with
4428
4459
`@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.
4432
4473
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.
4435
4476
Consequently, no other `ParameterResolver` registered with JUnit Jupiter can resolve
4436
4477
parameters for such a constructor.
4437
4478
@@ -4477,6 +4518,26 @@ In the following example, Spring injects the `OrderService` bean from the
4477
4518
4478
4519
Note that this feature lets test dependencies be `final` and therefore immutable.
4479
4520
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
+
4480
4541
[[testcontext-junit-jupiter-di-method]]
4481
4542
====== Method Injection
4482
4543
0 commit comments