Skip to content

Commit fff2291

Browse files
committed
Document context lifecycle & logging semantics for the TestContext framework
This commit documents ApplicationContext initialization and shutdown logging semantics for console output triggered in conjunction with the Spring TestContext Framework. Closes gh-25385
1 parent 917f3ad commit fff2291

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/docs/asciidoc/testing.adoc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4584,6 +4584,52 @@ context. Note that support for the `@DirtiesContext` annotation is provided by t
45844584
`DirtiesContextBeforeModesTestExecutionListener` and the
45854585
`DirtiesContextTestExecutionListener`, which are enabled by default.
45864586

4587+
.ApplicationContext lifecycle and console logging
4588+
[NOTE]
4589+
====
4590+
When you need to debug a test executed with the Spring TestContext Framework, it can be
4591+
useful to analyze the console output (that is, output to the `SYSOUT` and `SYSERR`
4592+
streams). Some build tools and IDEs are able to associate console output with a given
4593+
test; however, some console output cannot be easily associated with a given test.
4594+
4595+
With regard to console logging triggered by the Spring Framework itself or by components
4596+
registered in the `ApplicationContext`, it is important to understand the lifecycle of an
4597+
`ApplicationContext` that has been loaded by the Spring TestContext Framework within a
4598+
test suite.
4599+
4600+
The `ApplicationContext` for a test is typically loaded when an instance of the test
4601+
class is being prepared -- for example, to perform dependency injection into `@Autowired`
4602+
fields of the test instance. This means that any console logging triggered during the
4603+
initialization of the `ApplicationContext` typically cannot be associated with an
4604+
individual test method. However, if the context is closed immediately before the
4605+
execution of a test method according to <<spring-testing-annotation-dirtiescontext>>
4606+
semantics, a new instance of the context will be loaded just prior to execution of the
4607+
test method. In the latter scenario, an IDE or build tool may potentially associate
4608+
console logging with the individual test method.
4609+
4610+
The `ApplicationContext` for a test can be closed via one of the following scenarios.
4611+
4612+
* The context is closed according to `@DirtiesContext` semantics.
4613+
* The context is closed because it has been automatically evicted from the cache
4614+
according to the LRU eviction policy.
4615+
* The context is closed via a JVM shutdown hook when the JVM for the test suite
4616+
terminates.
4617+
4618+
If the context is closed according to `@DirtiesContext` semantics after a particular test
4619+
method, an IDE or build tool may potentially associate console logging with the
4620+
individual test method. If the context is closed according to `@DirtiesContext` semantics
4621+
after a test class, any console logging triggered during the shutdown of the
4622+
`ApplicationContext` cannot be associated with an individual test method. Similarly, any
4623+
console logging triggered during the shutdown phase via a JVM shutdown hook cannot be
4624+
associated with an individual test method.
4625+
4626+
When a Spring `ApplicationContext` is closed via a JVM shutdown hook, callbacks executed
4627+
during the shutdown phase are executed on a thread named `SpringContextShutdownHook`. So,
4628+
if you wish to disable console logging triggered when the `ApplicationContext` is closed
4629+
via a JVM shutdown hook, you may be able to register a custom filter with your logging
4630+
framework that allows you to ignore any logging initiated by that thread.
4631+
====
4632+
45874633
[[testcontext-ctx-management-ctx-hierarchies]]
45884634
===== Context Hierarchies
45894635

0 commit comments

Comments
 (0)