what am I doing?
I have an app that I want to test across different environments - dev, staging, etc
What I do?
I am using maven cargo
plugin to deploy application war to run integration tests.
What I need?
I need to my test to infer the spring.profiles.active
based on environment variable set by cargo like
<container>
<containerId>tomcat7x</containerId>
<systemProperties>
<spring.profiles.active>development</spring.profiles.active>
</systemProperties>
</container>
Why?
so that I can remove the hard coding in my Integration Test @ActiveProfiles("development")
and the test can infer what active profile is from environment variable
Question
- I found Spring integration tests with profile which mentions about using ActiveProfilesResolver
- I tried to find how can I make use of it, but could not find any resources
I am looking for guidance/recommendations on how to use ActiveProfilesResolver
In the following application, we have three profiles (local, dev, prod) and two profile-specific property files. We use the spring. profiles. active to set active profiles and SpringApplicationBuilder's profiles method to add new active profiles.
Use @Profile on a Bean Let's start simple and look at how we can make a bean belong to a particular profile. We use the @Profile annotation — we are mapping the bean to that particular profile; the annotation simply takes the names of one (or multiple) profiles.
Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded: @Configuration @Profile("production") public class ProductionConfiguration { // ... }
Profiles work in conjunction with Spring Boot properties files. By default, Spring Boot parses a file called application. properties – located in the src/main/resources directory – to identify configuration information.
Try this. I learned from different places and created the following. It works for me
public class SpringActiveProfileResolver implements ActiveProfilesResolver {
@Override
public String[] resolve(final Class<?> aClass) {
final String activeProfile = System.getProperty("spring.profiles.active");
return new String[] { activeProfile == null ? "integration" : activeProfile };
}
}
and on you test class do this
@ActiveProfiles(resolver = SpringActiveProfileResolver.class)
public class IT1DataSetup {
......
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With