How to Use Test Profile in Spring Boot for Isolated Testing
In Spring Boot, use
@ActiveProfiles("test") on your test classes to activate the test profile. This loads configuration and beans defined specifically for testing, isolating them from other profiles like dev or prod. You can define test-specific properties in application-test.properties or application-test.yml.Syntax
Use the @ActiveProfiles annotation on your test class to specify which Spring profile to activate during testing.
@ActiveProfiles("test"): Activates thetestprofile.@SpringBootTest: Loads the full application context for integration testing.- Define test-specific properties in
src/main/resources/application-test.propertiesorapplication-test.yml.
java
@SpringBootTest @ActiveProfiles("test") public class MyServiceTest { // test methods here }
Example
This example shows a Spring Boot test class that activates the test profile to load test-specific configuration and beans.
java
package com.example.demo; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest @ActiveProfiles("test") public class GreetingServiceTest { @Autowired private GreetingService greetingService; @Test void testGreetingMessage() { String message = greetingService.greet(); assertThat(message).isEqualTo("Hello from Test Profile!"); } } // GreetingService.java package com.example.demo; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @Service @Profile("test") public class GreetingService { public String greet() { return "Hello from Test Profile!"; } }
Output
Tests pass successfully with greeting message: "Hello from Test Profile!"
Common Pitfalls
Common mistakes when using test profiles in Spring Boot:
- Not annotating test classes with
@ActiveProfiles("test"), so the test profile is not activated. - Forgetting to create
application-test.propertiesor test-specific beans with@Profile("test"). - Using
@Profileon beans but not activating the matching profile in tests. - Relying on default profile configuration which can cause tests to use production settings unintentionally.
java
/* Wrong: No @ActiveProfiles annotation, test profile not activated */ @SpringBootTest public class WrongTest { // This will load default profile, not test } /* Right: Activate test profile explicitly */ @SpringBootTest @ActiveProfiles("test") public class RightTest { // Loads test profile beans and properties }
Quick Reference
| Concept | Usage | Notes |
|---|---|---|
| @ActiveProfiles | Annotate test class with @ActiveProfiles("test") | Activates the test profile during tests |
| application-test.properties | Place test-specific properties here | Overrides default properties when test profile is active |
| @Profile("test") | Annotate beans to load only in test profile | Ensures beans are only created during tests |
| @SpringBootTest | Loads full Spring Boot context for integration tests | Combine with @ActiveProfiles for profile-specific tests |
Key Takeaways
Use @ActiveProfiles("test") on test classes to activate the test profile.
Define test-specific properties in application-test.properties or application-test.yml.
Annotate beans with @Profile("test") to load them only during tests.
Without activating the test profile, tests may use default or production settings.
Combine @SpringBootTest with @ActiveProfiles for full context test isolation.