Skip to content

Commit bd9d333

Browse files
committed
Document effect of @DirtiesContext when used with constructor injection
Issue: SPR-17654
1 parent 9ea618e commit bd9d333

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/docs/asciidoc/testing.adoc

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,9 +3120,10 @@ the underlying context cache, you can set the log level for the
31203120
In the unlikely case that a test corrupts the application context and requires reloading
31213121
(for example, by modifying a bean definition or the state of an application object), you
31223122
can annotate your test class or test method with `@DirtiesContext` (see the discussion of
3123-
`@DirtiesContext` in <<integration-testing-annotations-spring>>). This instructs Spring
3123+
`@DirtiesContext` in <<spring-testing-annotation-dirtiescontext>>). This instructs Spring
31243124
to remove the context from the cache and rebuild the application context before running
3125-
the next test. Note that support for the `@DirtiesContext` annotation is provided by the
3125+
the next test that requires the same application context. Note that support for the
3126+
`@DirtiesContext` annotation is provided by the
31263127
`DirtiesContextBeforeModesTestExecutionListener` and the
31273128
`DirtiesContextTestExecutionListener`, which are enabled by default.
31283129

@@ -3287,11 +3288,10 @@ shows this configuration scenario:
32873288
NOTE: If you use `@DirtiesContext` in a test whose context is configured as part of a
32883289
context hierarchy, you can use the `hierarchyMode` flag to control how the context cache
32893290
is cleared. For further details, see the discussion of `@DirtiesContext` in
3290-
<<integration-testing-annotations-spring,Spring Testing Annotations>> and the
3291+
<<spring-testing-annotation-dirtiescontext,Spring Testing Annotations>> and the
32913292
{api-spring-framework}/test/annotation/DirtiesContext.html[`@DirtiesContext`] javadoc.
32923293
--
32933294

3294-
32953295
[[testcontext-fixture-di]]
32963296
==== Dependency Injection of Test Fixtures
32973297

@@ -4382,7 +4382,7 @@ and integration tests and simultaneously reap the benefits of the TestContext fr
43824382
such as support for loading application contexts, dependency injection of test instances,
43834383
transactional test method execution, and so on.
43844384

4385-
Furthermore, thanks to the rich extension API in JUnit Jupiter, Spring can provide the
4385+
Furthermore, thanks to the rich extension API in JUnit Jupiter, Spring provides the
43864386
following features above and beyond the feature set that Spring supports for JUnit 4 and
43874387
TestNG:
43884388

@@ -4417,7 +4417,7 @@ The following code listing shows how to configure a test class to use the
44174417
----
44184418
====
44194419

4420-
Since you can also use annotations in JUnit 5 as meta-annotations, Spring can provide the
4420+
Since you can also use annotations in JUnit 5 as meta-annotations, Spring provides the
44214421
`@SpringJUnitConfig` and `@SpringJUnitWebConfig` composed annotations to simplify the
44224422
configuration of the test `ApplicationContext` and JUnit Jupiter.
44234423

@@ -4492,6 +4492,25 @@ Spring assumes the responsibility for resolving _all_ parameters in the construc
44924492
Consequently, no other `ParameterResolver` registered with JUnit Jupiter can resolve
44934493
parameters for such a constructor.
44944494

4495+
[WARNING]
4496+
====
4497+
Constructor injection for test classes must not be used in conjunction with JUnit
4498+
Jupiter's `@TestInstance(PER_CLASS)` support if `@DirtiesContext` is used to close the
4499+
test's `ApplicationContext` before or after test methods.
4500+
4501+
The reason is that `@TestInstance(PER_CLASS)` instructs JUnit Jupiter to cache the test
4502+
instance between test method invocations. Consequently, the test instance will retain
4503+
references to beans that were originally injected from an `ApplicationContext` that has
4504+
been subsequently closed. Since the constructor for the test class will only be invoked
4505+
once in such scenarios, dependency injection will not occur again, and subsequent tests
4506+
will interact with beans from the closed `ApplicationContext` which may result in errors.
4507+
4508+
To use `@DirtiesContext` with "before test method" or "after test method" modes in
4509+
conjunction with `@TestInstance(PER_CLASS)`, one must configure dependencies from Spring
4510+
to be supplied via field or setter injection so that they can be re-injected between test
4511+
method invocations.
4512+
====
4513+
44954514
In the following example, Spring injects the `OrderService` bean from the
44964515
`ApplicationContext` loaded from `TestConfig.class` into the
44974516
`OrderServiceIntegrationTests` constructor.

0 commit comments

Comments
 (0)