Skip to content

Commit 9ea618e

Browse files
committed
Improve documentation for DI options in the TestContext framework
1 parent 2a76a65 commit 9ea618e

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/docs/asciidoc/testing.adoc

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,23 +3297,30 @@ is cleared. For further details, see the discussion of `@DirtiesContext` in
32973297

32983298
When you use the `DependencyInjectionTestExecutionListener` (which is configured by
32993299
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.
33093316

33103317
Because `@Autowired` is used to perform <<core.adoc#beans-factory-autowire, autowiring by
33113318
type>>, if you have multiple bean definitions of the same type, you cannot rely on this
33123319
approach for those particular beans. In that case, you can use `@Autowired` in
33133320
conjunction with `@Qualifier`. As of Spring 3.0, you can also choose to use `@Inject` in
33143321
conjunction with `@Named`. Alternatively, if your test class has access to its
33153322
`ApplicationContext`, you can perform an explicit lookup by using (for example) a call to
3316-
`applicationContext.getBean("titleRepository")`.
3323+
`applicationContext.getBean("titleRepository", TitleRepository.class)`.
33173324

33183325
If you do not want dependency injection applied to your test instances, do not annotate
33193326
fields or setter methods with `@Autowired` or `@Inject`. Alternatively, you can disable
@@ -3329,7 +3336,7 @@ is presented after all sample code listings.
33293336
[NOTE]
33303337
====
33313338
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.
33333340
33343341
The following examples make calls to static assertion methods, such as `assertNotNull()`,
33353342
but without prepending the call with `Assert`. In such cases, assume that the method was

0 commit comments

Comments
 (0)