-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Document that spring.profiles.active is ignored by @ActiveProfiles
#36600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.0.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,12 @@ bean definition profiles programmatically by implementing a custom | |
| xref:testing/testcontext-framework/ctx-management/env-profiles.adoc#testcontext-ctx-management-env-profiles-ActiveProfilesResolver[`ActiveProfilesResolver`] | ||
| and registering it by using the `resolver` attribute of `@ActiveProfiles`. | ||
|
|
||
| NOTE: The `spring.profiles.active` system property is not taken into account by the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although it is usually set as a JVM system property, it's also supported as an environment variable. So, we should revise the wording here. |
||
| Test Context Framework when determining active profiles for a test class. If you need | ||
| 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]. | ||
|
|
||
| See xref:testing/testcontext-framework/ctx-management/env-profiles.adoc[Context Configuration with Environment Profiles], | ||
| xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-nested-test-configuration[`@Nested` test class configuration], and the | ||
| {spring-framework-api}/test/context/ActiveProfiles.html[`@ActiveProfiles`] javadoc for | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -543,3 +543,74 @@ Kotlin:: | |
| ---- | ||
| ====== | ||
|
|
||
| The following example demonstrates how to implement and register a custom | ||
| `SystemPropertyActiveProfilesResolver` that allows the `spring.profiles.active` | ||
| system property to override profiles configured via `@ActiveProfiles`: | ||
sbrannen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [tabs] | ||
| ====== | ||
| Java:: | ||
| + | ||
| [source,java,indent=0,subs="verbatim,quotes",role="primary"] | ||
| ---- | ||
| // profiles resolved programmatically via a custom resolver that | ||
| // allows "spring.profiles.active" to override @ActiveProfiles | ||
| @ActiveProfiles( | ||
| resolver = SystemPropertyActiveProfilesResolver.class, | ||
| inheritProfiles = false) | ||
| class TransferServiceTest extends AbstractIntegrationTest { | ||
| // test body | ||
| } | ||
| ---- | ||
|
|
||
| Kotlin:: | ||
| + | ||
| [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] | ||
| ---- | ||
| // profiles resolved programmatically via a custom resolver that | ||
| // allows "spring.profiles.active" to override @ActiveProfiles | ||
| @ActiveProfiles( | ||
| resolver = SystemPropertyActiveProfilesResolver::class, | ||
| inheritProfiles = false) | ||
| class TransferServiceTest : AbstractIntegrationTest() { | ||
| // test body | ||
| } | ||
| ---- | ||
| ====== | ||
|
|
||
| [tabs] | ||
| ====== | ||
| Java:: | ||
| + | ||
| [source,java,indent=0,subs="verbatim,quotes",role="primary"] | ||
| ---- | ||
| public class SystemPropertyActiveProfilesResolver implements ActiveProfilesResolver { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming is hard. 😉 Perhaps Got any better ideas? |
||
|
|
||
| @Override | ||
| public String[] resolve(Class<?> testClass) { | ||
| String profiles = System.getProperty("spring.profiles.active"); | ||
| if (profiles != null && !profiles.isBlank()) { | ||
| return profiles.split(","); | ||
| } | ||
| return new DefaultActiveProfilesResolver().resolve(testClass); | ||
| } | ||
| } | ||
| ---- | ||
|
|
||
| Kotlin:: | ||
| + | ||
| [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] | ||
| ---- | ||
| class SystemPropertyActiveProfilesResolver : ActiveProfilesResolver { | ||
|
|
||
| override fun resolve(testClass: Class<*>): Array<String> { | ||
| val profiles = System.getProperty("spring.profiles.active") | ||
| if (!profiles.isNullOrBlank()) { | ||
| return profiles.split(",").toTypedArray() | ||
| } | ||
| return DefaultActiveProfilesResolver().resolve(testClass) | ||
| } | ||
| } | ||
| ---- | ||
| ====== | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,14 @@ | |
| * <p>This annotation will be inherited from an enclosing test class by default. | ||
| * See {@link NestedTestConfiguration @NestedTestConfiguration} for details. | ||
| * | ||
| * <p>Note that the {@code spring.profiles.active} system property is not taken | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as my first comment.
|
||
| * into account by the Test Context Framework when determining active profiles for | ||
| * a test class. If you need to override the profiles configured via | ||
| * {@code @ActiveProfiles}, you can implement a custom {@link ActiveProfilesResolver} | ||
| * and register it via the {@link #resolver} attribute. See | ||
| * <em>Context Configuration with Environment Profiles</em> in the reference manual | ||
| * for further details and examples. | ||
| * | ||
| * @author Sam Brannen | ||
| * @since 3.1 | ||
| * @see SmartContextLoader | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually is taken into account if you don't declare
@ActiveProfiles-- right?