This example shows how to use @ActiveProfiles in JUnit tests to select a profile. Also check out Spring Core Testing related to Profiles
Example
Controller
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ExampleController {
@Autowired
private GreetingService greetingService;
@GetMapping("/")
@ResponseBody
public String handle() {
return greetingService.getGreetingMsg();
}
}
GreetingService
package com.logicbig.example;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
public interface GreetingService {
String getGreetingMsg();
@Service
@Profile("default")
class LocalGreetingService implements GreetingService {
@Override
public String getGreetingMsg() {
return "hi from local environment";
}
}
@Service
@Profile("dev")
class DevGreetingService implements GreetingService {
@Override
public String getGreetingMsg() {
return "hi from dev";
}
}
@Service
@Profile("prod")
class ProductionGreetingService implements GreetingService {
@Override
public String getGreetingMsg() {
return "hi from production";
}
}
}
ControllerTest
package com.logicbig.example;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitWebConfig(AppConfig.class)
@ActiveProfiles("dev")
public class ControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvcTester mockMvcTester;
@BeforeEach
public void setup() {
mockMvcTester = MockMvcTester.from(webApplicationContext);
}
@Test
public void testGreetDevProfile() {
assertThat(mockMvcTester.get().uri("/"))
.hasStatusOk()
.bodyText()
.isEqualTo("hi from dev");
}
}
Output$ mvn test -Dtest="ControllerTest" [INFO] Scanning for projects... [INFO] [INFO] ------< com.logicbig.example:spring-mvc-setting-profile-in-tests >------ [INFO] Building spring-mvc-setting-profile-in-tests 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ spring-mvc-setting-profile-in-tests --- [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\profiles\spring-mvc-setting-profile-in-tests\src\main\resources [INFO] [INFO] --- compiler:3.3:compile (default-compile) @ spring-mvc-setting-profile-in-tests --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 4 source files to D:\example-projects\spring-mvc\profiles\spring-mvc-setting-profile-in-tests\target\classes [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ spring-mvc-setting-profile-in-tests --- [INFO] skip non existing resourceDirectory D:\example-projects\spring-mvc\profiles\spring-mvc-setting-profile-in-tests\src\test\resources [INFO] [INFO] --- compiler:3.3:testCompile (default-testCompile) @ spring-mvc-setting-profile-in-tests --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\example-projects\spring-mvc\profiles\spring-mvc-setting-profile-in-tests\target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ spring-mvc-setting-profile-in-tests --- [INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ControllerTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.493 s -- in com.logicbig.example.ControllerTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.640 s [INFO] Finished at: 2026-08-22T18:44:39+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- spring-test 7.0.6 (Spring TestContext Framework)
- junit-jupiter-engine 6.0.3 (Module "junit-jupiter-engine" of JUnit)
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- hamcrest 3.0 (Core API and libraries of hamcrest matcher framework)
- assertj-core 3.26.3 (Rich and fluent assertions for testing in Java)
- JDK 25
- Maven 3.9.11
|