Skip to content

Commit 3e96cab

Browse files
committed
Document parallel test execution support in the reference manual
Issue: SPR-5863
1 parent 6efb166 commit 3e96cab

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/asciidoc/testing.adoc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3502,6 +3502,58 @@ be automatically rolled back by the `TransactionalTestExecutionListener` (see
35023502
----
35033503

35043504

3505+
[[testcontext-support-classes]]
3506+
==== Parallel test execution
3507+
3508+
Spring Framework 5.0 introduces basic support for executing tests in parallel within a
3509+
single JVM when using the _Spring TestContext Framework_. In general this means that most
3510+
test classes or test methods can be executed in parallel without any changes to test code
3511+
or configuration.
3512+
3513+
[TIP]
3514+
====
3515+
For details on how to set up parallel test execution, consult the documentation for your
3516+
testing framework, build tool, or IDE.
3517+
====
3518+
3519+
Keep in mind that the introduction of concurrency into your test suite can result in
3520+
unexpected side effects, strange runtime behavior, and tests that only fail intermittently
3521+
or seemingly randomly. The Spring Team therefore provides the following general guidelines
3522+
for when __not__ to execute tests in parallel.
3523+
3524+
__Do not execute tests in parallel if:__
3525+
3526+
* Tests make use of Spring's `@DirtiesContext` support.
3527+
* Tests make use of JUnit 4's `@FixMethodOrder` support or any testing framework feature
3528+
that is designed to ensure that test methods execute in a particular order. Note,
3529+
however, that this does not apply if entire test classes are executed in parallel.
3530+
* Tests change the state of shared services or systems such as a database, message broker,
3531+
filesystem, etc. This applies to both in-memory and external systems.
3532+
3533+
[TIP]
3534+
====
3535+
If parallel test execution fails with an exception stating that the `ApplicationContext`
3536+
for the current test is no longer active, this typically means that the
3537+
`ApplicationContext` was removed from the `ContextCache` in a different thread.
3538+
3539+
This may be due to the use of `@DirtiesContext` or due to automatic eviction from the
3540+
`ContextCache`. If `@DirtiesContext` is the culprit, you will either need to find a way
3541+
to avoid using `@DirtiesContext` or exclude such tests from parallel execution. If the
3542+
maximum size of the `ContextCache` has been exceeded, you can increase the maximum size
3543+
of the cache. See the discussion on <<testcontext-ctx-management-caching,context
3544+
caching>> for details.
3545+
====
3546+
3547+
[WARNING]
3548+
====
3549+
Parallel test execution in the Spring TestContext Framework is only possible if the
3550+
underlying `TestContext` implementation provides a _copy constructor_ as explained in the
3551+
javadocs for `TestContext`. The `DefaultTestContext` used in Spring provides such a
3552+
constructor; however, if you use a third-party library that provides a custom
3553+
`TestContext` implementation, you will need to verify if it is suitable for parallel test
3554+
execution.
3555+
====
3556+
35053557
[[testcontext-support-classes]]
35063558
==== TestContext Framework support classes
35073559

0 commit comments

Comments
 (0)