10

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

Community
  • 1
  • 1
daydreamer
  • 87,243
  • 191
  • 450
  • 722

1 Answers1

19

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 {
......
}
daydreamer
  • 87,243
  • 191
  • 450
  • 722