Reputation: 2913
Each of my tests is annotated with
@TestPropertySource(locations = "classpath:application-test.properties")
The content of application-test.properties
starts with spring.profiles.active=..
. If this is set to testdev
, it will override properties by matching properties found in application-testdev.properties
and analogously if set to testuat
, it will override with application-testuat.properties
.
Some of the tests however really only make sense if testuat
is set, so I annotated them additionally with @ActiveProfiles("testuat")
. When I run it with spring.profiles.active=testdev
set in application-test.properties
it actually seems to ignore the testuat
properties and only read the base properties and the testdev
properties.
Is there a way to override spring.active.profiles
with @ActiveProfiles
?
Thanks for the help
Upvotes: 3
Views: 1682
Reputation: 17992
By default the @ActiveProfiles
takes precedence over the spring.actives.profiles
setting, however you can implement your own ActiveProfilesResolver
with more flexible behavior. Below is an example implementation that defaults to test
and optionally adds a second profile if set as a System property.
Replace @ActiveProfiles("testuat")
with @ActiveProfiles(resolver = SpringActiveProfilesResolver.class)
And add this class somewhere in your test codebase:
public class SpringActiveProfilesResolver implements ActiveProfilesResolver {
@Override
public String[] resolve(Class<?> aClass) {
List<String> springProfiles = Lists.newArrayList("test");
String systemSpringProfile = System.getProperty("spring.profiles.active");
if(StringUtils.isNotBlank(systemSpringProfile)) {
springProfiles.add(systemSpringProfile);
}
return springProfiles.toArray(String[]::new);
}
}
Upvotes: 3