From d7d02926546a26d40b728f352d8a784843400597 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 9 Jun 2023 11:41:20 +0200 Subject: [PATCH] Document @DirtiesContext semantics when declared at class & method level Closes gh-30623 --- .../integration-spring/annotation-dirtiescontext.adoc | 6 +++++- .../org/springframework/test/annotation/DirtiesContext.java | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-dirtiescontext.adoc b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-dirtiescontext.adoc index dde3455788..39c27a8f16 100644 --- a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-dirtiescontext.adoc +++ b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-dirtiescontext.adoc @@ -12,7 +12,11 @@ metadata. You can use `@DirtiesContext` as both a class-level and a method-level annotation within the same class or class hierarchy. In such scenarios, the `ApplicationContext` is marked as dirty before or after any such annotated method as well as before or after the current -test class, depending on the configured `methodMode` and `classMode`. +test class, depending on the configured `methodMode` and `classMode`. When +`@DirtiesContext` is declared at both the class level and the method level, the +configured modes from both annotations will be honored. For example, if the class mode is +set to `BEFORE_EACH_TEST_METHOD` and the method mode is set to `AFTER_METHOD`, the +context will be marked as dirty both before and after the given test method. The following examples explain when the context would be dirtied for various configuration scenarios: diff --git a/spring-test/src/main/java/org/springframework/test/annotation/DirtiesContext.java b/spring-test/src/main/java/org/springframework/test/annotation/DirtiesContext.java index bca018aa6b..a4ee691a20 100644 --- a/spring-test/src/main/java/org/springframework/test/annotation/DirtiesContext.java +++ b/spring-test/src/main/java/org/springframework/test/annotation/DirtiesContext.java @@ -39,6 +39,12 @@ import java.lang.annotation.Target; * {@code ApplicationContext} will be marked as dirty before or * after any such annotated method as well as before or after the current test * class, depending on the configured {@link #methodMode} and {@link #classMode}. + * When {@code @DirtiesContext} is declared at both the class level and the + * method level, the configured test phases from both annotations will be honored. + * For example, if the class mode is set to {@link ClassMode#BEFORE_EACH_TEST_METHOD + * BEFORE_EACH_TEST_METHOD} and the method mode is set to + * {@link MethodMode#AFTER_METHOD AFTER_METHOD}, the context will be marked as + * dirty both before and after the given test method. * *

Supported Test Phases

*