As other people have already pointed out, you can opt to use Maven to set the spring.profiles.active
system property, making sure not to use @ActiveProfiles
, but that's not convenient for tests run within the IDE.
For a programmatic means to set the active profiles, you have a few options.
- Spring 3.1: write a custom
ContextLoader
that prepares the context by setting active profiles in the context'sEnvironment
. - Spring 3.2: a custom
ContextLoader
remains an option, but a better choice is to implement anApplicationContextInitializer
and configure it via theinitializers
attribute of@ContextConfiguration
. Your custom initializer can configure theEnvironment
by programmatically setting the active profiles. - Spring 4.0: the aforementioned options still exist; however, as of Spring Framework 4.0 there is a new dedicated
ActiveProfilesResolver
API exactly for this purpose: to programmatically determine the set of active profiles to use in a test. AnActiveProfilesResolver
can be registered via theresolver
attribute of@ActiveProfiles
.
Regards,
Sam (author of the Spring TestContext Framework)