@ActiveProfiles Example in Spring Test

By Arvind Rai, January 27, 2019
@ActiveProfiles is a class-level annotation that is used to activate profiles while loading ApplicationContext in Spring integration test. @ActiveProfiles has following elements.
profiles: Specify profiles to activate.
resolver: Specify ActiveProfilesResolver to activate profiles programmatically.
value: It is alias for profiles element.
inheritProfiles: Boolean value to decide whether active profiles should be inherited from superclass or not. The default value is true.

@ActiveProfiles are used in Spring integration test as following.
@ExtendWith(SpringExtension.class)
@ActiveProfiles("prod")
@ContextConfiguration
public class ActiveProfileTest {
------
} 
To Activate multiple profiles using @ActiveProfiles, specify profiles as an array.
@ExtendWith(SpringExtension.class)
@ActiveProfiles({ "prod", "lion" })
@ContextConfiguration
public class ActiveProfileTest {
------
} 
Here on this page we will create Spring integration test classes using @ActiveProfiles annotation with complete example.

Technologies Used

Find the technologies being used in our example.
1. Java 11
2. Spring 5.1.3.RELEASE
3. Spring Boot 2.1.1.RELEASE
4. JUnit 5
5. Maven 3.5.2
6. Eclipse 2018-09

Using @ActiveProfiles

Here we will create an application in which we have two JavaConfig with profile name dev and prod and we have two animal components with profile name lion and elephant. Find the test class with @ActiveProfiles annotation that activates prod and lion profiles.
ActiveProfileTest1.java
package com.concretepage;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.concretepage.config.AppConfigDev;
import com.concretepage.config.AppConfigProd;
import com.concretepage.services.Animal;

@ExtendWith(SpringExtension.class)
@ActiveProfiles({ "prod", "lion" })
@ContextConfiguration(classes = { AppConfigProd.class, AppConfigDev.class })
public class ActiveProfileTest1 {
	@Autowired
	private Animal animal;
	
	@Autowired
	private User user;	

	@Test
	public void messageTest() throws Exception {
		assertTrue("Hello Lion!".equals(animal.getMessage()));
	}

	@Test
	public void userTest() throws Exception {
		assertTrue(100 == user.getId());
		assertTrue("Prod User".equals(user.getName()));
	}
} 

Find the main files of the application.
AppConfigDev.java
package com.concretepage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.concretepage.User;

@Configuration
@ComponentScan("com.concretepage")
@Profile("dev")
public class AppConfigDev {
	@Bean
	public User getUser() {
		return new User(200, "Dev User");
	}
} 
AppConfigProd.java
package com.concretepage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.concretepage.User;

@Configuration
@ComponentScan("com.concretepage")
@Profile("prod")
public class AppConfigProd {
  @Bean
  public User getUser(){
	return new User(100, "Prod User");
  }
} 
User.java
package com.concretepage;
public class User {
	private Integer id;
	private String name;

	public User(Integer id, String name) {
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public String getName() {
		return name;
	}
}
Animal.java
package com.concretepage.services;
public interface Animal {
	String getMessage();
} 
Elephant.java
package com.concretepage.services;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Profile("elephant")
public class Elephant implements Animal {
	@Override
	public String getMessage() {
		return "Hello Elephant!";
	}

}
Lion.java
package com.concretepage.services;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Profile("lion")
public class Lion implements Animal {
	@Override
	public String getMessage() {
		return "Hello Lion!";
	}
} 
pom.xml
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
	</dependency>	
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-api</artifactId>
		<version>5.3.2</version>
		<scope>test</scope>
	</dependency>	
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-engine</artifactId>
		<version>5.3.2</version>
		<scope>test</scope>
	</dependency>	
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-params</artifactId>
		<version>5.3.2</version>
		<scope>test</scope>
	</dependency>		    
	<dependency>
		<groupId>org.junit.platform</groupId>
		<artifactId>junit-platform-launcher</artifactId>
		<version>1.3.2</version>
		<scope>test</scope>
	</dependency>	    	    		
</dependencies> 

@ActiveProfiles in Inheritance

@ActiveProfiles has inheritProfiles element which accepts Boolean value that decides whether active profiles should be inherited from superclass or not. The default value is true. Here in our example we have two test classes out of which one extends another. The super class has activated only lion and child class has activated prod profile.
ActiveProfileTest2.java
package com.concretepage;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.concretepage.config.AppConfigDev;
import com.concretepage.config.AppConfigProd;
import com.concretepage.services.Animal;

@ExtendWith(SpringExtension.class)
@ActiveProfiles("lion")
@ContextConfiguration(classes = { AppConfigProd.class, AppConfigDev.class })
class AnotherProfileTest {
	@Autowired
	private Animal animal;

	@Test
	public void messageTest1() throws Exception {
		assertTrue("Hello Lion!".equals(animal.getMessage()));
	}
}

@ActiveProfiles("prod")
public class ActiveProfileTest2 extends AnotherProfileTest {
	@Autowired
	private Animal animal;
	
	@Autowired
	private User user;	

	@Test
	public void messageTest2() throws Exception {
		assertTrue("Hello Lion!".equals(animal.getMessage()));
	}

	@Test
	public void userTest() throws Exception {
		assertTrue(100 == user.getId());
		assertTrue("Prod User".equals(user.getName()));
	}
} 
In the child class lion profile will also be inherited because the default value of inheritProfiles is true. Find the print screen of the output.
@ActiveProfiles Example in Spring Test

@ActiveProfiles in Spring Boot

Here we will use @ActiveProfiles annotation with @SpringBootTest annotation in Spring Boot integration test. The @SpringBootTest annotation is used at class-level to create a test class in Spring Boot application.
ActiveProfileTest.java
package com.concretepage;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.ActiveProfiles;
import com.concretepage.services.Animal;

@ActiveProfiles({ "prod", "lion" })
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ActiveProfileTest {
	@Autowired
	private Animal animal;
	@Autowired
	private TestRestTemplate restTemplate;

	@Test
	public void serviceTest() {
		String message = animal.getMessage();
		assertTrue("Hello Lion!".equals(message));
	}

	@Test
	public void webAppTest() {
		String url = "http://localhost:8585/spring-boot-prod/";
		String body = this.restTemplate.getForObject(url, String.class);
		assertTrue("Hello Lion!".equals(body));
	}
} 

Now find the main files of the application.
DevCustomizer.java
package com.concretepage.config;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("dev")
public class DevCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
	@Override
	public void customize(ConfigurableServletWebServerFactory factory) {
		factory.setContextPath("/spring-boot-dev");
		factory.setPort(8484);
	}
}
ProdCustomizer.java
package com.concretepage.config;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("prod")
public class ProdCustomizer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
	@Override
	public void customize(ConfigurableServletWebServerFactory factory) {
		factory.setContextPath("/spring-boot-prod");
		factory.setPort(8585);	
	}
} 
HelloController.java
package com.concretepage.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.concretepage.services.Animal;

@RestController
public class HelloController {
   @Autowired
   private Animal animal;
	
   @GetMapping("/")	
   public String getMessage() {
	   return animal.getMessage();
   }
} 

References

Spring Doc: @ActiveProfiles
Spring Testing

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us