@ -1189,6 +1189,7 @@ The following annotations are supported only when used in conjunction with the
@@ -1189,6 +1189,7 @@ The following annotations are supported only when used in conjunction with the
@ -1281,6 +1282,35 @@ See <<testcontext-ctx-management>> as well as the javadoc for
@@ -1281,6 +1282,35 @@ See <<testcontext-ctx-management>> as well as the javadoc for
@ -1386,6 +1416,7 @@ You can use each of the following as a meta-annotation in conjunction with the
@@ -1386,6 +1416,7 @@ You can use each of the following as a meta-annotation in conjunction with the
* `@ProfileValueSourceConfiguration` _(only supported on JUnit 4)_
* `@SpringJUnitConfig` _(only supported on JUnit Jupiter)_
* `@SpringJUnitWebConfig` _(only supported on JUnit Jupiter)_
* `@TestConstructor` _(only supported on JUnit Jupiter)_
* `@EnabledIf` _(only supported on JUnit Jupiter)_
* `@DisabledIf` _(only supported on JUnit Jupiter)_
@ -4423,15 +4454,25 @@ Specifically, `SpringExtension` can inject dependencies from the test's
@@ -4423,15 +4454,25 @@ Specifically, `SpringExtension` can inject dependencies from the test's
[[testcontext-junit-jupiter-di-constructor]]
====== Constructor Injection
If a parameter in a constructor for a JUnit Jupiter test class is of type
If a specific parameter in a constructor for a JUnit Jupiter test class is of type
`ApplicationContext` (or a sub-type thereof) or is annotated or meta-annotated with
`@Autowired`, `@Qualifier`, or `@Value`, Spring injects the value for that specific
parameter with the corresponding bean from the test's `ApplicationContext`. You can also
directly annotate a test constructor with `@Autowired` if all of the parameters should be
supplied by Spring.
parameter with the corresponding bean or value from the test's `ApplicationContext`.
Spring can also be configured to autowire all arguments for a test class constructor if
the constructor is considered to be _autowirable_. A constructor is considered to be
autowirable if one of the following conditions is met (in order of precedence).
* The constructor is annotated with `@Autowired`.
* `@TestConstructor` is present or meta-present on the test class with the `autowire`
attribute set to `true`.
* The default _test constructor autowire_ mode is set to `true`.
See <<integration-testing-annotations-testconstructor>> for details on the use of
`@TestConstructor` and how to set the global _test constructor autowire_ mode.
WARNING: If the constructor for a test class is itself annotated with `@Autowired`,
Spring assumes the responsibility for resolving _all_ parameters in the constructor.
WARNING: If the constructor for a test class is considered to be _autowirable_, Spring
assumes the responsibility for resolving arguments for all parameters in the constructor.
Consequently, no other `ParameterResolver` registered with JUnit Jupiter can resolve
parameters for such a constructor.
@ -4477,6 +4518,26 @@ In the following example, Spring injects the `OrderService` bean from the
@@ -4477,6 +4518,26 @@ In the following example, Spring injects the `OrderService` bean from the
Note that this feature lets test dependencies be `final` and therefore immutable.
If the `spring.test.constructor.autowire` property is to `true` (see
<<integration-testing-annotations-testconstructor>>), we can omit the declaration of
`@Autowired` on the constructor in the previous example resulting in the following.