Repository navigation
Closed as not planned
Closed as not planned
Copy link
Labels
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: testIssues in the test modulestatus: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Description
Alex opened SPR-8982 and commented
Status Quo
Support for setting the active bean definition profiles in an integration test within the Spring TestContext Framework (TCF) is currently limited to one of the following techniques:
- Providing a hard-coded list of profiles declared via the
valueorprofilesattribute of@ActiveProfiles- since Spring Framework 3.1 - Specifying a custom
ActiveProfilesResolver(which programmatically determines the active profiles) declared via theresolverattribute of@ActiveProfiles- since Spring Framework 4.0
In stand-alone or production code, Spring honors the spring.profiles.active system property for determining the set of active profiles to use when loading an ApplicationContext; however, the TCF ignores this system property, relying solely on configuration via @ActiveProfiles.
Proposals
- Provide the ability to override
@ActiveProfilesspecified in an integration test via thespring.profiles.activesystem property or some other system property specific to the TCF. - Set the
spring.profiles.activesystem property to the value specified via@ActiveProfiles- See comments in this issue for background.
- This point likely warrants its own JIRA issue for proper consideration.
Implementation Notes
- Consider introducing a new
booleanflag in@ActiveProfilesthat allows profiles declared via the annotation to be overridden by those defined via thespring.profiles.activesystem property (if it is present).
Further Resources
Related discussions at Stack Overflow:
- Spring integration tests with profile
- Defining a spring active profile within a test use case
- @ActiveProfiles value is not being assigned to config
Affects: 3.1 GA
Issue Links:
- Decide what to do with
@IfProfileValue[SPR-7754] #12410 Decide what to do with@IfProfileValue - Introduce strategy for determining if a profile value is enabled for a particular test environment [SPR-4862] #9538 Introduce strategy for determining if a profile value is enabled for a particular test environment
SystemProfileValueSourceis not very compatible with the new 3.1 default system property profiles [SPR-8985] #13625 SystemProfileValueSource is not very compatible with the new 3.1 default system property profiles- Introduce annotation to disable test based on active Spring profile [SPR-11677] #16300 Introduce annotation to skip test based on active Spring profile
- spring.profiles.active is not honored when building the context cache key in the TestContext framework [SPR-11135] #15761 spring.profiles.active is not honored when building the context cache key in the TestContext framework
19 votes, 24 watchers
Reacted by Simone Vratogna, Bojan Trajkovski, Tom W., Mohammed Adem bourenane, Justin Kromlinger, Kai Wang, Martin Tarjányi, Michael Olšavský and Meris Nici
Metadata
Metadata
Assignees
Labels
has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: testIssues in the test modulestatus: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Activity
spring-projects-issues commentedon Mar 6, 2013
Peter De Winter commented
That or it should set the property spring.profiles.active.
spring-projects-issues commentedon Mar 6, 2013
Sam Brannen commented
Peter De Winter, what do you mean by it?
spring-projects-issues commentedon Mar 6, 2013
Peter De Winter commented
When running tests with a certain active profile the environment property
spring.profiles.activeis not being set.Using
${spring.profiles.active}placeholders inside your XML namespace application context this placeholder is not resolved when launching tests with@ActiveProfiles.Example:
Now that I read the issue description again it might be this is not completely same request, but the issue reason might be the same...
spring-projects-issues commentedon Jan 12, 2014
Martin Flower commented
I would also like the
@ActiveProfilesannotation to set the value ofspring.profiles.active. This will enable me to writeAnd my integration tests will inherit from a parent class annotated with
@ActiveProfiles("integrationtest")and will pick up anAppConfigPropertieswhich is configured usingprops/integrationtest/configuration.properties.At the moment I cannot see how to set
spring.profiles.activefrom the integration test base class (extendsAbstractTransactionalTestNGSpringContextTests) as theApplicationContextseems to be loaded before I can call any processing.spring-projects-issues commentedon Jan 31, 2014
Sam Brannen commented
Martin Flower,
If you need to set the
spring.profiles.activesystem property before the TestContext framework loads yourApplicationContext, you could always do so in a before class method. With TestNG, this would be in an@BeforeClassmethod, and you could likely achieve the same with an@BeforeSuiteor@BeforeTestmethod.Regards,
Sam
spring-projects-issues commentedon Feb 4, 2014
Martin Flower commented
Thanks Sam. At the moment I have the following abstract base classes
So, I'm using a static {} block. My original intention was to use
@ActiveProfilesannotation instead, which I couldn't get to work.Would be putting this code in a
@BeforeClassmethod be an improvement? Maybe ? : )/Martin
spring-projects-issues commentedon Feb 4, 2014
Peter De Winter commented
Thanks Sam Brannen. This was indeed a solution to our specific problem.
spring-projects-issues commentedon Feb 4, 2014
Sam Brannen commented
Martin Flower, if you've already got that code in static blocks, then there isn't really any functional difference between that and using an
@BeforeClassmethod. The only difference is that using an@BeforeClassmethod might make the intent more obvious. ;)spring-projects-issues commentedon Feb 4, 2014
Sam Brannen commented
Peter De Winter, I'm glad that little trick solves your problem!
In fact, it might remain the only way to have the system property set. Even if we implement the original intent of this issue (i.e., allowing the presence of the system property to override
@ActiveProfiles), it might be the case that Spring does not set the system property for you. Of course, the details have not yet been decided, but I am leaning toward not setting the system property since that could have adverse side effects on other tests running within the same JVM.Regards,
Sam
spring-projects-issues commentedon Feb 4, 2014
Sam Brannen commented
For further ideas on how to set active profiles programmatically, you may be interested in my response to the Spring integration tests with profile thread on Stack Overflow.
spring-projects-issues commentedon Oct 3, 2016
Grigory Kislin commented
This is the sample of possible fix: http://stackoverflow.com/a/33044283/548473
spring-projects-issues commentedon Jan 12, 2017
Sam Brannen commented
GKislin, indeed... that's a nice custom work-around.
Thanks for sharing!
spring-projects-issues commentedon Jan 12, 2017
Wim Deblauwe commented
Voting for this as I have this question: http://stackoverflow.com/questions/41614368
spring-projects-issues commentedon Feb 9, 2018
Hussein Yapit commented
Spring has been pretty consistent with runtime configuration (like system/env properties) trumping over compile-time config, except for this case. I argue it's a better design to allow systemProperty override
@ActiveProfilesby default, i.e. without having to specify a boolean param as suggested in the description.Alternative proposal includes make DefaultActiveProfilesResolver to be a pass-through if spring.profiles.active has been specified.
4 remaining items
sfussenegger commentedon Mar 16, 2022
here's a simple implementation that might help with support for
spring.profiles.activeandspring.profiles.include:Usage with unit tests:
Example usage from Maven
snicoll commentedon Jan 2, 2024
I am not sure about that. Being able to specify the value you want that particular test to use shouldn't be overruled by something in your environment. If we did that, then your test could fail based on external factors. IMO, if we introduce a way to tune that externally, it probably should be with its own property. Slice tests in Spring Boot use the
spring.testnamespace for this. Perhapsspring.test.profilesor something?Overriding the property looks in order but I am not sure the property should be set if it wasn't
sfussenegger commentedon Jan 4, 2024
@snicoll I think with the maven-failsafe-plugin use-case there is even an argument to be made for adding something like
@ActiveProfiles(profiles = "testing", profileIncludeProperty = "spring.test.profiles.include", includeProfiles = true)to allow for different profile inclusions in different (integration) tests across a test suite. For instance if I'd have MySQL- and PostgreSQL-specific code I'd like to test I might need to include"mysql"or"pgsql"accordingly. Having only a single pre-defined property would mean I'd have to configure multiple runs with different property values.snicoll commentedon Jan 4, 2024
OK but that's a different issue altogether. We're discussing the merits of being able to change the list of profiles even if one is set in the tests using a property. You seem to be asking for something else. I didn't get the reference with failsafe nor what that code snippet would do but that looks overcomplicated to me.
sfussenegger commentedon Jan 4, 2024
@snicoll Not sure about the "we're discussing" when the last comment other than mine is from 2018 ;) If there is an ongoing discussion in a related ticket that brought you hear I'm not aware of it.
Anyway, jut like you I'd respectfully disagree with the comment by Hussein you've been quoting. I think in most projects people will run multiple tests in a single test suite. Not sure how often they'd actually want to overrides profiles for all of these tests. Maybe some would. In our projects I think we'd be running into a bunch of new issues with unwanted profile changes.
The original ticket suggested in "Implementation Notes" to "Consider introducing a new
booleanflag" which is what I'd prefer and what is actually achieved by using theConfigurableActiveProfilesResolverclass I've attached in 2022. This code could therefore be merged intoDefaultActiveProfilesResolver(plus handling for that newbooleanflag that should be turned off by default).My last comment was indeed an extension to that, suggesting using some kind of
propertyNameproperty rather than a hard-coded property to allow for different profiles in different tests in a single test suit run. So that would be an extra feature. Also, I'd consider supporting setting active profiles as well as profile inclusion (the oldspring.profiles.includeproperty vsspring.profiles.active). That was theincludeProfiles = truein my previous comment. So either choose override or include. Again, both properties are supported in theConfigurableActiveProfilesResolverposted above (though the constants might no longer be available in newer spring or spring-boot versions.snicoll commentedon Jan 4, 2024
I am talking about this issue and the person who raised it. I believe I am right in assuming you're not "Alex" and that's what I mean by a different issue altogether as what you're suggesting has a far more wider scope for something we haven't even decided to consider.
spring.profiles.includeis a Spring Boot specific feature, so not to be considered here. I'd like we keep the discussion to the scope as raised by the OP, at least for now.sfussenegger commentedon Jan 4, 2024
[-]Allow overriding @ActiveProfiles in test classes with system property [SPR-8982][/-][+]Allow overriding `@ActiveProfiles` in test classes with system property [SPR-8982][/+][-]Allow overriding `@ActiveProfiles` in test classes with system property [SPR-8982][/-][+]Allow overriding @ActiveProfiles in test classes with system property [SPR-8982][/+][-]Allow overriding @ActiveProfiles in test classes with system property [SPR-8982][/-][+]Allow overriding `@ActiveProfiles` in test classes with system property [SPR-8982][/+]sbrannen commentedon May 6, 2024
Team Decision
After further consideration, the team has decided not to set the
spring.profiles.activesystem property automatically inDefaultActiveProfilesResolver, since that can break the configuration and runtime behavior for other test classes within the same test suite.In addition, we will not provide built-in support for overriding profiles configured via
@ActiveProfileswith a system property.Instead, we encourage developers to rely on the standard semantics for
@ActiveProfilesor implement a customActiveProfilesResolver(introduced in Spring Framework 4.0 in conjunction with #14972) that meets the specific needs of the project.Various examples of custom
ActiveProfilesResolverimplementations have been provided in the comments in this issue, on Stack Overflow, and elsewhere.If you need to switch the profiles in various situations, another option is to avoid the use of
@ActiveProfilesaltogether and instead consistently set the necessaryspring.profiles.activesystem property or make use of profile features in Spring Boot in your build or IDE.In light of the above, we are closing this issue.