So confusingly @IfProfileValue
has nothing to do with @Profile
or @ActiveProfiles
. @Profile
tests to see if a profile is active, @ActiveProfiles
sets them as active, and @IfProfileValue
allows you to check things in Spring Environment
. Wut? I'd deprecate all of them and add new ones @IfEnvironment
, @IfProfile
, and @ActivateProfiles
.
Commentary aside, how can I use @IfProfileValue
to detect whether i have a profile active? I am not using Spring Boot on this project, at this time. Answers should show code, and we will assume that I want the test to run if the profile is activated as @ActiveProfiles( "test" )
.
I tried @IfProfileValue(name = "activeProfiles", value = "test")
but that seems to have the test skipped, which means it's not matching. I'm going to speculate the problem may have to do with the fact that ActiveProfiles
is a Collection
.
That's correct, and I explained this in detail here: https://stackoverflow.com/a/23627479/388980
... which I'm assuming you have already seen, since you commented on my answer yesterday.
The reason that
@IfProfileValue
has nothing to do with@Profile
or@ActiveProfiles
is due to the evolution of the framework. See below for further details.These statements are not entirely correct, especially the last part.
@Profile
is used to selectively enable a component (e.g.,@Service
, etc.),@Configuration
class, or@Bean
method if one of the named bean definition profiles is active in the SpringEnvironment
for theApplicationContext
. This annotation is not directly related to testing:@Profile
should not be used on a test class.@ActiveProfiles
is used to designate which bean definition profiles (e.g., those declared via@Profile
) should be active when loading anApplicationContext
for an integration test.@IfProfileValue
does not allow you to check things in the SpringEnvironment
. I'm not sure why you are assuming this, since none of the documentation in the Spring Framework states that. As I stated in the aforementioned thread:Please note that
@IfProfileValue
was introduced in Spring Framework 2.0, long before the notion of bean definition profiles, and@ActiveProfiles
was first introduced in Spring Framework 3.1.In the aforementioned thread, I also pointed out the following:
The term 'profile' is perhaps misleading when considering the semantics for
@IfProfileValue
. The key is to think about 'test groups' (like those in TestNG) instead of 'profiles'. See the examples in the JavaDoc for @IfProfileValue.That depends, and... I'm assuming you mean bean definition profile when you say "profile".
If you're using
@ActiveProfiles
to set the bean definition profiles for your tests, you cannot currently use@IfProfileValue
to determine if a bean definition profile is active, since the bean definition profiles configured via@ActiveProfiles
are set directly in the test'sApplicationContext
and not as a Java system property.However, if you are setting the bean definition profiles only via the
spring.profiles.active
system property, then it would be possible to use@IfProfileValue
to determine if a bean definition profile is active, since@IfProfileValue
in fact works with system properties. For example, you could then use the following:That won't work since
activeProfiles
is the incorrect property name. The correct system property name isspring.profiles.active
. SeeAbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME
for details.The fact that
@IfProfileValue
does not work in harmony with@ActiveProfiles
is a known issue to the Spring team. Please consult the following JIRA issues for further details and to join in on the discussions if you'd like.Hope this clarifies the situation for you!
Sam (author of the Spring TestContext Framework)
Sam nailed it. (As well as the fact this was accepted and answered years back)
One thing to add is that if you'd like to pass System Properties through to your test, having them propagate through to the JVM if you are using a build tool like gradle may require an additional step.
And then business as usual in your integration test
Finally you can execute your tests from the terminal with the property you specified.
The thing I like most about
@IfProfileValue
over grabbing the System.property and checkingassumeTrue/False
manually is that no Spring Context is loaded (or flyway/other migrations you may have) keeping unit tests fast.