The @ActiveProfiles annotation is a way to tell your integration tests exactly which profiles to use (check out this tutorial on @Profile).
Definition of ActiveProfilesVersion: 7.0.3 package org.springframework.test.context;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ActiveProfiles {
@AliasFor("profiles")
String[] value() default {}; 1
@AliasFor("value")
String[] profiles() default {}; 2
Class<? extends ActiveProfilesResolver> resolver()
default ActiveProfilesResolver.class; 3
boolean inheritProfiles() default true; 4
}
Example
Creating a Spring Profile based application
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile(PROFILE_LOCAL)
public DataService dataServiceLocal() {
return DataServiceLocal.Instance;
}
@Bean
@Profile(PROFILE_REMOTE)
public DataService dataServiceRemote() {
return DataServiceRemote.Instance;
}
public static final String PROFILE_LOCAL = "local";
public static final String PROFILE_REMOTE = "remote";
}
package com.logicbig.example;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface DataService {
List<Customer> getCustomersByAge(int minAge, int maxAge);
}
package com.logicbig.example;
public class Customer {
private long customerId;
private String name;
private int age;
.............
}
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile(PROFILE_LOCAL)
public DataService dataServiceLocal() {
return DataServiceLocal.Instance;
}
@Bean
@Profile(PROFILE_REMOTE)
public DataService dataServiceRemote() {
return DataServiceRemote.Instance;
}
public static final String PROFILE_LOCAL = "local";
public static final String PROFILE_REMOTE = "remote";
}
The JUnit test
package com.logicbig.example;
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 java.util.List;
import static org.springframework.test.util.AssertionErrors.assertTrue;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = AppConfig.class)
@ActiveProfiles(AppConfig.PROFILE_LOCAL)
public class DataServiceTests {
@Autowired
private DataService dataService;
@Test
public void testCustomerAges() {
List<Customer> customers = dataService.getCustomersByAge(25, 40);
for (Customer customer : customers) {
int age = customer.getAge();
assertTrue("Age range is not 25 to 40: " + customer,
age >= 25 && age < 40);
}
}
}
OutputD:\example-projects\spring-core-testing\spring-test-active-profile>mvn test -Dtest=DataServiceTests [INFO] Scanning for projects... [INFO] [INFO] ----------< com.logicbig.example:spring-test-active-profile >----------- [INFO] Building spring-test-active-profile 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ spring-test-active-profile --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-core-testing\spring-test-active-profile\src\main\resources [INFO] [INFO] --- compiler:3.3:compile (default-compile) @ spring-test-active-profile --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 5 source files to D:\example-projects\spring-core-testing\spring-test-active-profile\target\classes [INFO] /D:/LogicBig/example-projects/spring-core-testing/spring-test-active-profile/src/main/java/com/logicbig/example/DataServiceRemote.java: D:\example-projects\spring-core-testing\spring-test-active-profile\src\main\java\com\logicbig\example\DataServiceRemote.java uses unchecked or unsafe operations. [INFO] /D:/LogicBig/example-projects/spring-core-testing/spring-test-active-profile/src/main/java/com/logicbig/example/DataServiceRemote.java: Recompile with -Xlint:unchecked for details. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ spring-test-active-profile --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\spring-core-testing\spring-test-active-profile\src\test\resources [INFO] [INFO] --- compiler:3.3:testCompile (default-testCompile) @ spring-test-active-profile --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to D:\example-projects\spring-core-testing\spring-test-active-profile\target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ spring-test-active-profile --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.DataServiceTests [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.727 s -- in com.logicbig.example.DataServiceTests [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.203 s [INFO] Finished at: 2026-01-31T13:34:43+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - spring-context 7.0.3 (Spring Context)
Version Compatibility: 5.0.0.RELEASE - 7.0.3 Version compatibilities of spring-context with this example: Versions in green have been tested.
- spring-test 7.0.3 (Spring TestContext Framework)
- junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
- JDK 25
- Maven 3.9.11
|