@@ -3120,9 +3120,10 @@ the underlying context cache, you can set the log level for the
3120
3120
In the unlikely case that a test corrupts the application context and requires reloading
3121
3121
(for example, by modifying a bean definition or the state of an application object), you
3122
3122
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
3124
3124
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
3126
3127
`DirtiesContextBeforeModesTestExecutionListener` and the
3127
3128
`DirtiesContextTestExecutionListener`, which are enabled by default.
3128
3129
@@ -3287,11 +3288,10 @@ shows this configuration scenario:
3287
3288
NOTE: If you use `@DirtiesContext` in a test whose context is configured as part of a
3288
3289
context hierarchy, you can use the `hierarchyMode` flag to control how the context cache
3289
3290
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
3291
3292
{api-spring-framework}/test/annotation/DirtiesContext.html[`@DirtiesContext`] javadoc.
3292
3293
--
3293
3294
3294
-
3295
3295
[[testcontext-fixture-di]]
3296
3296
==== Dependency Injection of Test Fixtures
3297
3297
@@ -4382,7 +4382,7 @@ and integration tests and simultaneously reap the benefits of the TestContext fr
4382
4382
such as support for loading application contexts, dependency injection of test instances,
4383
4383
transactional test method execution, and so on.
4384
4384
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
4386
4386
following features above and beyond the feature set that Spring supports for JUnit 4 and
4387
4387
TestNG:
4388
4388
@@ -4417,7 +4417,7 @@ The following code listing shows how to configure a test class to use the
4417
4417
----
4418
4418
====
4419
4419
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
4421
4421
`@SpringJUnitConfig` and `@SpringJUnitWebConfig` composed annotations to simplify the
4422
4422
configuration of the test `ApplicationContext` and JUnit Jupiter.
4423
4423
@@ -4492,6 +4492,25 @@ Spring assumes the responsibility for resolving _all_ parameters in the construc
4492
4492
Consequently, no other `ParameterResolver` registered with JUnit Jupiter can resolve
4493
4493
parameters for such a constructor.
4494
4494
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
+
4495
4514
In the following example, Spring injects the `OrderService` bean from the
4496
4515
`ApplicationContext` loaded from `TestConfig.class` into the
4497
4516
`OrderServiceIntegrationTests` constructor.
0 commit comments