For a test class, when it uses @ActiveProfiles it works as expected by applying the profile(s) defined through the profiles attribute. But it is not possible to override this via a JVM system propety: -Dspring.profiles.active=....
For example:
- Override
@ActiveProfiles({"dev","cache"}) by -Dspring.profiles.active=prod,-no-cache
Of course, if the test class comments out @ActiveProfiles({"dev","cache"}), then -Dspring.profiles.active=prod,-no-cache works in peace.
Is there an official explicit indication and explanation about the following?
- Why is it not possible to override
@ActiveProfiles via -Dspring.profiles.active=...?
- How to accomplish the desired goal?
I already did some research in the official documentation at:
And there is nothing about how to resolve 1 and 2.
According to my understanding, to accomplish (2):
- Implement the ActiveProfilesResolver interface to take into consideration the
spring.profiles.active option over the profiles element. Of course only if the former exists, otherwise the latter works in peace (practically the current behavior of the unique and default implementation: the DefaultActiveProfilesResolver class).
- Finally choose this implementation through the
resolver attribute as @ActiveProfiles(profiles={"dev","cache"}, resolver=ImprovedActiveProfilesResolver.class)
Thanks for your understanding.
For a test class, when it uses
@ActiveProfilesit works as expected by applying the profile(s) defined through theprofilesattribute. But it is not possible to override this via a JVM system propety:-Dspring.profiles.active=....For example:
@ActiveProfiles({"dev","cache"})by-Dspring.profiles.active=prod,-no-cacheOf course, if the test class comments out
@ActiveProfiles({"dev","cache"}), then-Dspring.profiles.active=prod,-no-cacheworks in peace.Is there an official explicit indication and explanation about the following?
@ActiveProfilesvia-Dspring.profiles.active=...?I already did some research in the official documentation at:
And there is nothing about how to resolve 1 and 2.
According to my understanding, to accomplish (2):
spring.profiles.activeoption over theprofileselement. Of course only if the former exists, otherwise the latter works in peace (practically the current behavior of the unique and default implementation: the DefaultActiveProfilesResolver class).resolverattribute as@ActiveProfiles(profiles={"dev","cache"}, resolver=ImprovedActiveProfilesResolver.class)Thanks for your understanding.