@@ -3297,23 +3297,30 @@ is cleared. For further details, see the discussion of `@DirtiesContext` in
3297
3297
3298
3298
When you use the `DependencyInjectionTestExecutionListener` (which is configured by
3299
3299
default), the dependencies of your test instances are injected from beans in the
3300
- application context that you configured with `@ContextConfiguration`. You may use setter
3301
- injection, field injection, or both, depending on which annotations you choose and
3302
- whether you place them on setter methods or fields. For consistency with the annotation
3303
- support introduced in Spring 2.5 and 3.0, you can use Spring's `@Autowired` annotation or
3304
- the `@Inject` annotation from JSR 330.
3305
-
3306
- TIP: The TestContext framework does not instrument the manner in which a test instance is
3307
- instantiated. Thus, the use of `@Autowired` or `@Inject` for constructors has no effect
3308
- for test classes.
3300
+ application context that you configured with `@ContextConfiguration` or related
3301
+ annotations. You may use setter injection, field injection, or both, depending on which
3302
+ annotations you choose and whether you place them on setter methods or fields. If you are
3303
+ using JUnit Jupiter you may also optionally use constructor injection (see
3304
+ <<testcontext-junit-jupiter-di>>). For consistency with the annotation support introduced
3305
+ in Spring 2.5 and 3.0, you can use Spring's `@Autowired` annotation or the `@Inject`
3306
+ annotation from JSR 330 for field and setter injection.
3307
+
3308
+ TIP: For testing frameworks other than JUnit Jupiter, the TestContext framework does not
3309
+ participate in instantiation of the test class. Thus, the use of `@Autowired` or
3310
+ `@Inject` for constructors has no effect for test classes.
3311
+
3312
+ NOTE: Although field injection is discouraged in production code, field injection is
3313
+ actually quite natural in test code. The rationale for the difference is that you will
3314
+ never instantiate your test class directly. Consequently, there is no need to be able to
3315
+ invoke a `public` constructor or setter method on your test class.
3309
3316
3310
3317
Because `@Autowired` is used to perform <<core.adoc#beans-factory-autowire, autowiring by
3311
3318
type>>, if you have multiple bean definitions of the same type, you cannot rely on this
3312
3319
approach for those particular beans. In that case, you can use `@Autowired` in
3313
3320
conjunction with `@Qualifier`. As of Spring 3.0, you can also choose to use `@Inject` in
3314
3321
conjunction with `@Named`. Alternatively, if your test class has access to its
3315
3322
`ApplicationContext`, you can perform an explicit lookup by using (for example) a call to
3316
- `applicationContext.getBean("titleRepository")`.
3323
+ `applicationContext.getBean("titleRepository", TitleRepository.class )`.
3317
3324
3318
3325
If you do not want dependency injection applied to your test instances, do not annotate
3319
3326
fields or setter methods with `@Autowired` or `@Inject`. Alternatively, you can disable
@@ -3329,7 +3336,7 @@ is presented after all sample code listings.
3329
3336
[NOTE]
3330
3337
====
3331
3338
The dependency injection behavior in the following code listings is not specific to JUnit
3332
- 4. The same DI techniques can be used in conjunction with any testing framework.
3339
+ 4. The same DI techniques can be used in conjunction with any supported testing framework.
3333
3340
3334
3341
The following examples make calls to static assertion methods, such as `assertNotNull()`,
3335
3342
but without prepending the call with `Assert`. In such cases, assume that the method was
0 commit comments