@ -3297,15 +3297,22 @@ is cleared. For further details, see the discussion of `@DirtiesContext` in
@@ -3297,15 +3297,22 @@ is cleared. For further details, see the discussion of `@DirtiesContext` in
When you use the `DependencyInjectionTestExecutionListener` (which is configured by
default), the dependencies of your test instances are injected from beans in the
application context that you configured with `@ContextConfiguration`. You may use setter
injection, field injection, or both, depending on which annotations you choose and
whether you place them on setter methods or fields. For consistency with the annotation
support introduced in Spring 2.5 and 3.0, you can use Spring's `@Autowired` annotation or
the `@Inject` annotation from JSR 330.
TIP: The TestContext framework does not instrument the manner in which a test instance is
instantiated. Thus, the use of `@Autowired` or `@Inject` for constructors has no effect
for test classes.
application context that you configured with `@ContextConfiguration` or related
annotations. You may use setter injection, field injection, or both, depending on which
annotations you choose and whether you place them on setter methods or fields. If you are
using JUnit Jupiter you may also optionally use constructor injection (see
<<testcontext-junit-jupiter-di>>). For consistency with the annotation support introduced
in Spring 2.5 and 3.0, you can use Spring's `@Autowired` annotation or the `@Inject`
annotation from JSR 330 for field and setter injection.
TIP: For testing frameworks other than JUnit Jupiter, the TestContext framework does not
participate in instantiation of the test class. Thus, the use of `@Autowired` or
`@Inject` for constructors has no effect for test classes.
NOTE: Although field injection is discouraged in production code, field injection is
actually quite natural in test code. The rationale for the difference is that you will
never instantiate your test class directly. Consequently, there is no need to be able to
invoke a `public` constructor or setter method on your test class.
Because `@Autowired` is used to perform <<core.adoc#beans-factory-autowire, autowiring by
type>>, if you have multiple bean definitions of the same type, you cannot rely on this
@ -3313,7 +3320,7 @@ approach for those particular beans. In that case, you can use `@Autowired` in
@@ -3313,7 +3320,7 @@ approach for those particular beans. In that case, you can use `@Autowired` in
conjunction with `@Qualifier`. As of Spring 3.0, you can also choose to use `@Inject` in
conjunction with `@Named`. Alternatively, if your test class has access to its
`ApplicationContext`, you can perform an explicit lookup by using (for example) a call to