Spring Profiles: A Simple Example of ActiveProfilesResolver?

What am I doing?
I have an application that I want to test in different environments - dev, staging, etc.

What am I doing?
I use the maven cargo plugin to deploy an application war to run integration tests.

What I need?
I need to run my test to draw spring.profiles.active based on the environment variable set by the load, e.g.

  <container> <containerId>tomcat7x</containerId> <systemProperties> <spring.profiles.active>development</spring.profiles.active> </systemProperties> </container> 

Why?
so that I can remove @ActiveProfiles("development") in my @ActiveProfiles("development") integration test, and the test can conclude which active profile is from the environment variable

Question
- I found Spring integration tests with a profile that mention using ActiveProfilesResolver
- I tried to find how I can use it, but could not find any resources
I am looking for guidance / recommendations for using ActiveProfilesResolver

+6
source share
1 answer

Try it. 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 in your test class do it

 @ActiveProfiles(resolver = SpringActiveProfileResolver.class) public class IT1DataSetup { ...... } 
+9
source

Source: https://habr.com/ru/post/969862/


All Articles