From 438b1011feacbf9966958aadb418e21396ccabff Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 9 Apr 2026 12:52:18 +0200 Subject: [PATCH] =?UTF-8?q?Revise=20documentation=20for=20@=E2=81=A0Active?= =?UTF-8?q?Profiles=20and=20ActiveProfilesResolver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See gh-36269 See gh-36600 (cherry picked from commit 99b78adce337c001986b2f79bc0d510e4570b49d) --- .../annotation-activeprofiles.adoc | 2 +- .../ctx-management/env-profiles.adoc | 40 ++++++++++++------- .../test/context/ActiveProfiles.java | 8 +++- .../test/context/ActiveProfilesResolver.java | 5 ++- .../DefaultActiveProfilesResolver.java | 10 +++-- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-activeprofiles.adoc b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-activeprofiles.adoc index c68bf29dd9b..43da00956a6 100644 --- a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-activeprofiles.adoc +++ b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-activeprofiles.adoc @@ -74,7 +74,7 @@ and registering it by using the `resolver` attribute of `@ActiveProfiles`. NOTE: When `@ActiveProfiles` is declared on a test class, the `spring.profiles.active` property (whether configured as a JVM system property or environment variable) is not -taken into account by the Test Context Framework when determining active profiles. If +taken into account by the TestContext Framework when determining active profiles. If you need to allow `spring.profiles.active` to override the profiles configured via `@ActiveProfiles`, you can implement a custom `ActiveProfilesResolver` as described in xref:testing/testcontext-framework/ctx-management/env-profiles.adoc[Context Configuration with Environment Profiles]. diff --git a/framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management/env-profiles.adoc b/framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management/env-profiles.adoc index fb997f5c6bd..2fcd2bb9488 100644 --- a/framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management/env-profiles.adoc +++ b/framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management/env-profiles.adoc @@ -545,8 +545,8 @@ Kotlin:: The following example demonstrates how to implement and register a custom `SystemPropertyOverrideActiveProfilesResolver` that allows the `spring.profiles.active` -property (whether configured as a JVM system property or environment variable) to -override profiles configured via `@ActiveProfiles`: +property (when configured as a JVM system property) to override profiles configured via +`@ActiveProfiles`: [tabs] ====== @@ -583,35 +583,45 @@ Kotlin:: ====== Java:: + -[source,java,indent=0,subs="verbatim,quotes",role="primary"] +[source,java,indent=0,subs="verbatim,quotes",role="primary",fold="-imports"] ---- - public class SystemPropertyOverrideActiveProfilesResolver implements ActiveProfilesResolver { + import org.springframework.core.env.AbstractEnvironment; + import org.springframework.test.context.support.DefaultActiveProfilesResolver; + import org.springframework.util.StringUtils; + + public class SystemPropertyOverrideActiveProfilesResolver extends DefaultActiveProfilesResolver { @Override public String[] resolve(Class testClass) { - String profiles = System.getProperty("spring.profiles.active"); - if (profiles != null && !profiles.isBlank()) { - return profiles.split(","); + String profiles = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME); + if (StringUtils.hasText(profiles)) { + return StringUtils.commaDelimitedListToStringArray( + StringUtils.trimAllWhitespace(profiles)); } - return new DefaultActiveProfilesResolver().resolve(testClass); + return super.resolve(testClass); } } ---- Kotlin:: + -[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] +[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary",fold="-imports"] ---- - class SystemPropertyOverrideActiveProfilesResolver : ActiveProfilesResolver { + import org.springframework.core.env.AbstractEnvironment + import org.springframework.test.context.support.DefaultActiveProfilesResolver + import org.springframework.util.StringUtils + + class SystemPropertyOverrideActiveProfilesResolver : DefaultActiveProfilesResolver() { override fun resolve(testClass: Class<*>): Array { - val profiles = System.getProperty("spring.profiles.active") - if (!profiles.isNullOrBlank()) { - return profiles.split(",").toTypedArray() + val profiles = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME) + if (StringUtils.hasText(profiles)) { + return StringUtils.commaDelimitedListToStringArray( + StringUtils.trimAllWhitespace(profiles) + ) } - return DefaultActiveProfilesResolver().resolve(testClass) + return super.resolve(testClass) } } ---- ====== - diff --git a/spring-test/src/main/java/org/springframework/test/context/ActiveProfiles.java b/spring-test/src/main/java/org/springframework/test/context/ActiveProfiles.java index 473ca7a5702..19572a1f2c8 100644 --- a/spring-test/src/main/java/org/springframework/test/context/ActiveProfiles.java +++ b/spring-test/src/main/java/org/springframework/test/context/ActiveProfiles.java @@ -38,8 +38,9 @@ import org.springframework.core.annotation.AliasFor; * See {@link NestedTestConfiguration @NestedTestConfiguration} for details. * *

Note that when {@code @ActiveProfiles} is declared on a test class, the - * {@code spring.profiles.active} property (whether configured as a JVM system - * property or environment variable) is not taken into account by the Test Context + * {@link org.springframework.core.env.AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME + * spring.profiles.active} property (whether configured as a JVM system property + * or environment variable) is not taken into account by the Spring TestContext * Framework when determining active profiles. If you need to allow * {@code spring.profiles.active} to override the profiles configured via * {@code @ActiveProfiles}, you can implement a custom {@link ActiveProfilesResolver} @@ -81,6 +82,9 @@ public @interface ActiveProfiles { /** * The type of {@link ActiveProfilesResolver} to use for resolving the active * bean definition profiles programmatically. + *

If not specified, the + * {@link org.springframework.test.context.support.DefaultActiveProfilesResolver + * DefaultActiveProfilesResolver} will be used. * @since 4.0 * @see ActiveProfilesResolver */ diff --git a/spring-test/src/main/java/org/springframework/test/context/ActiveProfilesResolver.java b/spring-test/src/main/java/org/springframework/test/context/ActiveProfilesResolver.java index 1840958afd3..2ba01c5a055 100644 --- a/spring-test/src/main/java/org/springframework/test/context/ActiveProfilesResolver.java +++ b/spring-test/src/main/java/org/springframework/test/context/ActiveProfilesResolver.java @@ -30,7 +30,8 @@ package org.springframework.test.context; * @author Sam Brannen * @author Michail Nikolaev * @since 4.0 - * @see ActiveProfiles + * @see ActiveProfiles @ActiveProfiles + * @see org.springframework.test.context.support.DefaultActiveProfilesResolver DefaultActiveProfilesResolver */ @FunctionalInterface public interface ActiveProfilesResolver { @@ -41,7 +42,7 @@ public interface ActiveProfilesResolver { * @param testClass the test class for which the profiles should be resolved; * never {@code null} * @return the bean definition profiles to use when loading the - * {@code ApplicationContext}; never {@code null} + * {@code ApplicationContext}; never {@code null} but potentially empty * @see ActiveProfiles#resolver * @see ActiveProfiles#inheritProfiles */ diff --git a/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java b/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java index 82693971830..d03861a1d39 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java @@ -32,10 +32,12 @@ import static org.springframework.test.context.TestContextAnnotationUtils.findAn * configured declaratively via {@link ActiveProfiles#profiles} or * {@link ActiveProfiles#value}. * - *

Note that the {@code spring.profiles.active} property (whether configured - * as a JVM system property or environment variable) is not taken into account by - * this resolver. If you need to allow {@code spring.profiles.active} to override - * profiles configured via {@link ActiveProfiles}, you can implement a custom + *

Note that the + * {@link org.springframework.core.env.AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME + * spring.profiles.active} property (whether configured as a JVM system property + * or environment variable) is not taken into account by this resolver. If you need + * to allow {@code spring.profiles.active} to override profiles configured via + * {@link ActiveProfiles @ActiveProfiles}, you can implement a custom * {@link ActiveProfilesResolver} and register it via {@link ActiveProfiles#resolver}. * * @author Sam Brannen