Closed as not planned
Closed as not planned
Copy link
Labels
in: testIssues in the test modulestatus: supersededAn issue that has been superseded by anothertype: enhancementA general enhancement
Description
Torsten Krah opened SPR-11677 and commented
I've looked at the @IfProfileValue and @ActiveProfiles annotations, but neither does what I am searching for.
We are using the "spring.profiles.active" system property to activate different profiles, even at the test level -- e.g. "h2, default" or something else.
We have tests which are only for Oracle and should therefore only be run if the "oracle" profile is active, but I've found no way to express this on the test class itself.
A dedicated annotation would be nice to support this.
Affects: 4.0.3
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
- Allow overriding
@ActiveProfilesin test classes with system property [SPR-8982] #13622 Allow overriding@ActiveProfilesin test classes with system property 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
8 votes, 8 watchers
Metadata
Metadata
Assignees
Labels
in: testIssues in the test modulestatus: supersededAn issue that has been superseded by anothertype: enhancementA general enhancement
Activity
spring-projects-issues commentedon Apr 9, 2014
Sam Brannen commented
Hi Torsten,
I've linked several related issues: #9538, #13622, #13625, #12410.
Can you please review them to see if your needs would be addressed by one of them?
Thanks,
Sam
spring-projects-issues commentedon Apr 9, 2014
Torsten Krah commented
If i read all of the issues right, they are about setting the profile in a test, i did not found a hint how to actually trigger the test class at all based on the active profile (which i did set already via System property when running the test suite
@jenkins) - like@Ignoreon the class or method - something like@RunWithSpringProfile("h2", "default") - or something.#9538 seems to be the same direction but has no solution yet. Feel free to correct me if i missed some comment at one task you've linked.
Thanks, Torsten.
spring-projects-issues commentedon Jul 11, 2014
Christoph Strobl commented
Hi,
We've had similar requirements when testing against different versions. Basically we created a custom JUnit
@Ruleto disable tests for features only available in Redis 2.8 when running against 2.6.Let me outline what I am thinking of in terms of dealing with spring profiles.
I've added a sample below basically adding a new annotation
IfSpringProfileActiveholding the desired profile name. TheSpringProfileRulechecks for the presence ofIfSpringProfileActive, loads the currentEnvironmentif not already set, and verifies that the desired profile is currently active.There's
SpringRuleTestat the end of the sample to show what it does.spring-projects-issues commentedon Oct 11, 2015
Caleb Cushing commented
Why not just allow test methods and test classes to be annotated with
@Profileand skip them based on the same rules beans would otherwise not be processed with?This is the most obvious solution to me, and is the first solution I reached for.
tauinger-de commentedon May 26, 2021
As mentioned above annotating test classes and methods with
@Profileis a possible solution and IMHO the most intuitive way. Basically I did that without thinking and was wondering why it doesn't work.[-]Introduce annotation to skip test based on active Spring profile [SPR-11677][/-][+]Introduce annotation to disable test based on active Spring profile [SPR-11677][/+]sbrannen commentedon Jan 10, 2024
Please note that it is already possible to enable/disable a test class or test method via a SpEL expression when using JUnit Jupiter (from JUnit 5).
Inspired by the test case supplied by @christophstrobl, I put together the following example.
The test class passes as-is, but if you set the active spring profile to
not-oraclethenshouldOnlyRunWhenActiveProfileSetToOracle()will be disabled, andshouldBeIgnoredIfActiveProfileSetToOracle()will be enabled and fail.The key thing to note is the use of the
@EnabledIf/@DisabledIfannotations fromspring-test(not the@EnabledIf/@DisabledIfannotations fromjunit-jupiter-api).Another very important thing to note is that it only works with
loadContext = true, and that means that the test'sApplicationContextwill be eagerly loaded even if it's potentially never needed.The latter is a downside to any approach I can think of to reliably "disable a test based on an active Spring profile".
Using
@Profileon test classes or test methods -- as suggested in various comments in this issue -- would have the same downside.sbrannen commentedon Jan 10, 2024
The benefit of using something like the
@EnabledIfexample above with a SpEL expression is that it matches bean definition profiles that have been registered in theEnvironmentfor the test's actualApplicationContext, and that's what I meant by "reliable".However, if all you want to do is match against the value of the
spring.profiles.activesystem property, you can do that with JUnit Jupiter's@EnabledIfSystemProperty/@DisabledIfSystemPropertysupport.The following example demonstrates how to achieve that.
sbrannen commentedon Jan 16, 2024
Note that
Environment.matchesProfiles(String...)supports profile expressions.Thus, the following:
is equivalent to:
I've updated the example in #16300 (comment) to highlight that.