Skip to content

Commit 99b78ad

Browse files
committed
Revise documentation for @⁠ActiveProfiles and ActiveProfilesResolver
See gh-36269 See gh-36600
1 parent 644731c commit 99b78ad

5 files changed

Lines changed: 41 additions & 24 deletions

File tree

framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-activeprofiles.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ and registering it by using the `resolver` attribute of `@ActiveProfiles`.
7474

7575
NOTE: When `@ActiveProfiles` is declared on a test class, the `spring.profiles.active`
7676
property (whether configured as a JVM system property or environment variable) is not
77-
taken into account by the Test Context Framework when determining active profiles. If
77+
taken into account by the TestContext Framework when determining active profiles. If
7878
you need to allow `spring.profiles.active` to override the profiles configured via
7979
`@ActiveProfiles`, you can implement a custom `ActiveProfilesResolver` as described in
8080
xref:testing/testcontext-framework/ctx-management/env-profiles.adoc[Context Configuration with Environment Profiles].

framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management/env-profiles.adoc

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ Kotlin::
545545

546546
The following example demonstrates how to implement and register a custom
547547
`SystemPropertyOverrideActiveProfilesResolver` that allows the `spring.profiles.active`
548-
property (whether configured as a JVM system property or environment variable) to
549-
override profiles configured via `@ActiveProfiles`:
548+
property (when configured as a JVM system property) to override profiles configured via
549+
`@ActiveProfiles`:
550550

551551
[tabs]
552552
======
@@ -583,35 +583,45 @@ Kotlin::
583583
======
584584
Java::
585585
+
586-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
586+
[source,java,indent=0,subs="verbatim,quotes",role="primary",fold="-imports"]
587587
----
588-
public class SystemPropertyOverrideActiveProfilesResolver implements ActiveProfilesResolver {
588+
import org.springframework.core.env.AbstractEnvironment;
589+
import org.springframework.test.context.support.DefaultActiveProfilesResolver;
590+
import org.springframework.util.StringUtils;
591+
592+
public class SystemPropertyOverrideActiveProfilesResolver extends DefaultActiveProfilesResolver {
589593
590594
@Override
591595
public String[] resolve(Class<?> testClass) {
592-
String profiles = System.getProperty("spring.profiles.active");
593-
if (profiles != null && !profiles.isBlank()) {
594-
return profiles.split(",");
596+
String profiles = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);
597+
if (StringUtils.hasText(profiles)) {
598+
return StringUtils.commaDelimitedListToStringArray(
599+
StringUtils.trimAllWhitespace(profiles));
595600
}
596-
return new DefaultActiveProfilesResolver().resolve(testClass);
601+
return super.resolve(testClass);
597602
}
598603
}
599604
----
600605
601606
Kotlin::
602607
+
603-
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
608+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary",fold="-imports"]
604609
----
605-
class SystemPropertyOverrideActiveProfilesResolver : ActiveProfilesResolver {
610+
import org.springframework.core.env.AbstractEnvironment
611+
import org.springframework.test.context.support.DefaultActiveProfilesResolver
612+
import org.springframework.util.StringUtils
613+
614+
class SystemPropertyOverrideActiveProfilesResolver : DefaultActiveProfilesResolver() {
606615
607616
override fun resolve(testClass: Class<*>): Array<String> {
608-
val profiles = System.getProperty("spring.profiles.active")
609-
if (!profiles.isNullOrBlank()) {
610-
return profiles.split(",").toTypedArray()
617+
val profiles = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)
618+
if (StringUtils.hasText(profiles)) {
619+
return StringUtils.commaDelimitedListToStringArray(
620+
StringUtils.trimAllWhitespace(profiles)
621+
)
611622
}
612-
return DefaultActiveProfilesResolver().resolve(testClass)
623+
return super.resolve(testClass)
613624
}
614625
}
615626
----
616627
======
617-

spring-test/src/main/java/org/springframework/test/context/ActiveProfiles.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
* See {@link NestedTestConfiguration @NestedTestConfiguration} for details.
3939
*
4040
* <p>Note that when {@code @ActiveProfiles} is declared on a test class, the
41-
* {@code spring.profiles.active} property (whether configured as a JVM system
42-
* property or environment variable) is not taken into account by the Test Context
41+
* {@link org.springframework.core.env.AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
42+
* spring.profiles.active} property (whether configured as a JVM system property
43+
* or environment variable) is not taken into account by the Spring TestContext
4344
* Framework when determining active profiles. If you need to allow
4445
* {@code spring.profiles.active} to override the profiles configured via
4546
* {@code @ActiveProfiles}, you can implement a custom {@link ActiveProfilesResolver}
@@ -81,6 +82,9 @@
8182
/**
8283
* The type of {@link ActiveProfilesResolver} to use for resolving the active
8384
* bean definition profiles programmatically.
85+
* <p>If not specified, the
86+
* {@link org.springframework.test.context.support.DefaultActiveProfilesResolver
87+
* DefaultActiveProfilesResolver} will be used.
8488
* @since 4.0
8589
* @see ActiveProfilesResolver
8690
*/

spring-test/src/main/java/org/springframework/test/context/ActiveProfilesResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
* @author Sam Brannen
3131
* @author Michail Nikolaev
3232
* @since 4.0
33-
* @see ActiveProfiles
33+
* @see ActiveProfiles @ActiveProfiles
34+
* @see org.springframework.test.context.support.DefaultActiveProfilesResolver DefaultActiveProfilesResolver
3435
*/
3536
@FunctionalInterface
3637
public interface ActiveProfilesResolver {
@@ -41,7 +42,7 @@ public interface ActiveProfilesResolver {
4142
* @param testClass the test class for which the profiles should be resolved;
4243
* never {@code null}
4344
* @return the bean definition profiles to use when loading the
44-
* {@code ApplicationContext}; never {@code null}
45+
* {@code ApplicationContext}; never {@code null} but potentially empty
4546
* @see ActiveProfiles#resolver
4647
* @see ActiveProfiles#inheritProfiles
4748
*/

spring-test/src/main/java/org/springframework/test/context/support/DefaultActiveProfilesResolver.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
* configured declaratively via {@link ActiveProfiles#profiles} or
3333
* {@link ActiveProfiles#value}.
3434
*
35-
* <p>Note that the {@code spring.profiles.active} property (whether configured
36-
* as a JVM system property or environment variable) is not taken into account by
37-
* this resolver. If you need to allow {@code spring.profiles.active} to override
38-
* profiles configured via {@link ActiveProfiles}, you can implement a custom
35+
* <p>Note that the
36+
* {@link org.springframework.core.env.AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
37+
* spring.profiles.active} property (whether configured as a JVM system property
38+
* or environment variable) is not taken into account by this resolver. If you need
39+
* to allow {@code spring.profiles.active} to override profiles configured via
40+
* {@link ActiveProfiles @ActiveProfiles}, you can implement a custom
3941
* {@link ActiveProfilesResolver} and register it via {@link ActiveProfiles#resolver}.
4042
*
4143
* @author Sam Brannen

0 commit comments

Comments
 (0)