Spring Boot - spring.profiles.active

Set Spring Profiles with spring.profiles.active

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

spring.profiles.active=qa

This application-qa.properties is the configuration for the Spring profile qa

host=www.qahost.com
username=qaUsername
password=qaPassword

application-production.properties is the configuration for the Spring profile production

host=www.productionhost.com
username=productionUsername
password=productionPassword

Map configuration for active Spring profile

Here we’re injecting properties from a corresponding application-{spring.profiles.active}.properties file

Props.java

package com.lenar.profiles.props;
										
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
										
@Component
public class Props {
										
	@Value("${host}")
	private String host;
										
	@Value("${username}")
	private String username;
										
	@Value("${password}")
	private String password;
										
	public String getHost() { return host; }
										
	public String getUsername() { return username; }
										
	public String getPassword() { return password; }
}

Application.java

										
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication
										
@SpringBootApplication
public class IntegrationTestApplication {
	public static void main(String[] args) {
		SpringApplication.run(IntegrationTestApplication.class, args);
	}	
}

Let’s try to use Spring profiles in TestNG test - SpringBootProfilesExampleTest.java.

										
import com.lenar.profiles.Application;
import com.lenar.profiles.props.Props;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
						
@SpringBootTest(IntegrationTestApplication.class)
public class SpringBootProfilesExampleTest extends AbstractTestNGSpringContextTests {
										
	@Autowired
	Props props;
										
	@Test
	public void printProfileSpecificConfiguratioTest() {
		System.out.println("Host: " + props.getHost());
		System.out.println("Username: " + props.getUsername ());
		System.out.println("Password: " + props.getPassword ());
	}
										
}

Spring maps properties from a corresponding application-{spring.profiles.active}.properties file to props.

Output for spring.profiles.active=qa

Host: www.qahost.com
Username: qaUsername
Password: qaPassword

Ways to switch Spring Boot profiles. spring.profiles.active

  1. Switch spring.profiles.active property in the application.properties file
  2. Switch spring.profiles.active with Maven:
mvn test -Dspring.profiles.active=production

Output when executed with spring.profiles.active=production

Host: www.productionhost.com
Username: productionUsername
Password: productionPassword								

You may also find these posts interesting: