Here is an example how to use Spring profiles in TestNG tests to switch between different environments. Saying that I mean to switch between different configurations for different environments. To do that we have to set spring.profiles.active environment variable, for example in your application.properties file.
Let’s create a simple Spring Boot project and add a TestNG test class and figure out how to set Spring profiles with spring.profiles.active.
Spring Profiles specific properties files
Add spring.profiles.active to your application.properties
This application-qa.properties is the configuration for the Spring profile qa
application-production.properties is the configuration for the Spring profile production
Map configuration for active Spring profile
Here we’re injecting properties from a corresponding application-{spring.profiles.active}.properties file
Props.java
Application.java
Let’s try to use Spring profiles in TestNG test - SpringBootProfilesExampleTest.java.
Spring maps properties from a corresponding application-{spring.profiles.active}.properties file to props.
Output for spring.profiles.active=qa
Ways to switch Spring Boot profiles. spring.profiles.active
Switch spring.profiles.active property in the application.properties file
Switch spring.profiles.active with Maven:
Output when executed with spring.profiles.active=production