0
0
SpringbootHow-ToBeginner · 3 min read

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 the test profile.
  • @SpringBootTest: Loads the full application context for integration testing.
  • Define test-specific properties in src/main/resources/application-test.properties or application-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.properties or test-specific beans with @Profile("test").
  • Using @Profile on 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

ConceptUsageNotes
@ActiveProfilesAnnotate test class with @ActiveProfiles("test")Activates the test profile during tests
application-test.propertiesPlace test-specific properties hereOverrides default properties when test profile is active
@Profile("test")Annotate beans to load only in test profileEnsures beans are only created during tests
@SpringBootTestLoads full Spring Boot context for integration testsCombine 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.