From f36327b380af813e0d5634f60fbe52443fc7ba8b Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 9 Jun 2023 11:37:08 +0200 Subject: [PATCH 1/4] Exclude commons-logging dependency in spring-test --- spring-test/spring-test.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-test/spring-test.gradle b/spring-test/spring-test.gradle index 70263f972a6..1daca60c167 100644 --- a/spring-test/spring-test.gradle +++ b/spring-test/spring-test.gradle @@ -36,6 +36,7 @@ dependencies { exclude group: "commons-logging", module: "commons-logging" } optional("org.seleniumhq.selenium:htmlunit-driver") { + exclude group: "commons-logging", module: "commons-logging" exclude group: "net.bytebuddy", module: "byte-buddy" } optional("org.seleniumhq.selenium:selenium-java") { From 698acd32a7d6bb5957efa05ab78b3d1400afa96c Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 9 Jun 2023 11:38:36 +0200 Subject: [PATCH 2/4] Configure logger per concrete @DirtiesContext TestExecutionListener --- .../support/AbstractDirtiesContextTestExecutionListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractDirtiesContextTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractDirtiesContextTestExecutionListener.java index 17586a70294..68eac510e5a 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractDirtiesContextTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractDirtiesContextTestExecutionListener.java @@ -48,7 +48,7 @@ import org.springframework.util.Assert; */ public abstract class AbstractDirtiesContextTestExecutionListener extends AbstractTestExecutionListener { - private static final Log logger = LogFactory.getLog(AbstractDirtiesContextTestExecutionListener.class); + private final Log logger = LogFactory.getLog(getClass()); @Override From 2817dce78f55274563ee3df3b9fdde42aec58875 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 9 Jun 2023 11:40:31 +0200 Subject: [PATCH 3/4] Polish @DirtiesContext Javadoc --- .../test/annotation/DirtiesContext.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) 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 9441c9133d3..bca018aa6b4 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,8 +31,8 @@ import java.lang.annotation.Target; * *

Use this annotation if a test has modified the context — for * example, by modifying the state of a singleton bean, modifying the state - * of an embedded database, etc. Subsequent tests that request the same - * context will be supplied a new context. + * of an embedded database, etc. Subsequent tests that request the same context + * will be supplied a new context. * *

{@code @DirtiesContext} may be used as a class-level and method-level * annotation within the same class or class hierarchy. In such scenarios, the @@ -40,14 +40,6 @@ import java.lang.annotation.Target; * after any such annotated method as well as before or after the current test * class, depending on the configured {@link #methodMode} and {@link #classMode}. * - *

This annotation may be used as a meta-annotation to create custom - * composed annotations. - * - *

As of Spring Framework 5.3, this annotation will be inherited from an - * enclosing test class by default. See - * {@link org.springframework.test.context.NestedTestConfiguration @NestedTestConfiguration} - * for details. - * *

Supported Test Phases

*
    *
  • Before current test class: when declared at the class @@ -70,9 +62,18 @@ import java.lang.annotation.Target; *
* *

{@code BEFORE_*} modes are supported by the - * {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener DirtiesContextBeforeModesTestExecutionListener}; - * {@code AFTER_*} modes are supported by the - * {@link org.springframework.test.context.support.DirtiesContextTestExecutionListener DirtiesContextTestExecutionListener}. + * {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener + * DirtiesContextBeforeModesTestExecutionListener}; {@code AFTER_*} modes are supported by the + * {@link org.springframework.test.context.support.DirtiesContextTestExecutionListener + * DirtiesContextTestExecutionListener}. + * + *

This annotation may be used as a meta-annotation to create custom + * composed annotations. + * + *

As of Spring Framework 5.3, this annotation will be inherited from an + * enclosing test class by default. See + * {@link org.springframework.test.context.NestedTestConfiguration @NestedTestConfiguration} + * for details. * * @author Sam Brannen * @author Rod Johnson From d7d02926546a26d40b728f352d8a784843400597 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 9 Jun 2023 11:41:20 +0200 Subject: [PATCH 4/4] 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 dde34557883..39c27a8f160 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 bca018aa6b4..a4ee691a207 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

*