The ActiveProfilesResolver interface allows you to programmatically determine which Spring profiles are active during a test.
Definition of ActiveProfilesResolverVersion: 7.0.3 package org.springframework.test.context;
@FunctionalInterface
public interface ActiveProfilesResolver {
String[] resolve(Class<?> testClass); 1
}
We need to register our custom resolver implementation with the resolver attribute of the @ActiveProfiles annotation. The following example demonstrates this process.
Example
Creating a simple Spring 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("windows")
public MyService myServiceA() {
return new MyServiceA();
}
@Bean
@Profile("other")
public MyService myServiceB() {
return new MyServiceB();
}
}
package com.logicbig.example;
public interface MyService {
public String doSomething();
}
package com.logicbig.example;
public class MyServiceA implements MyService {
@Override
public String doSomething() {
return "in " + this.getClass().getSimpleName();
}
}
package com.logicbig.example;
public class MyServiceB implements MyService {
@Override
public String doSomething() {
return "in " + this.getClass().getSimpleName();
}
}
Implementing MyActiveProfilesResolver
package com.logicbig.example;
import org.springframework.test.context.ActiveProfilesResolver;
public class MyActiveProfilesResolver implements ActiveProfilesResolver{
@Override
public String[] resolve(Class<?> testClass) {
String os = System.getProperty("os.name");
String profile = (os.toLowerCase().startsWith("windows")) ? "windows" : "other";
return new String[]{profile};
}
}
The JUnit test
Following test will fail if run other than Windows O.S.
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 static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = AppConfig.class)
@ActiveProfiles(resolver = MyActiveProfilesResolver.class)
public class MyServiceTests {
@Autowired
private MyService dataService;
@Test
public void testDoSomething() {
String s = dataService.doSomething();
System.out.println(s);
assertEquals("in MyServiceA", s);
}
}
Output$ mvn test -Dtest=MyServiceTests [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:active-profile-resolver-example >-------- [INFO] Building active-profile-resolver-example 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ active-profile-resolver-example --- [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\active-profile-resolver-example\src\main\resources [INFO] [INFO] --- compiler:3.3:compile (default-compile) @ active-profile-resolver-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ active-profile-resolver-example --- [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\active-profile-resolver-example\src\test\resources [INFO] [INFO] --- compiler:3.3:testCompile (default-testCompile) @ active-profile-resolver-example --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ active-profile-resolver-example --- [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.MyServiceTests in MyServiceA [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.694 s -- in com.logicbig.example.MyServiceTests [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.299 s [INFO] Finished at: 2026-02-01T11:59:56+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
|