To configure test profiles in Spring Boot, create an application-test.properties (or application-test.yml) file in src/test/resources and activate the profile in your tests with @ActiveProfiles("test"). Properties in the profile file override the defaults from application.properties, and beans annotated with @Profile("test") are only registered while the test profile is active.
The sections below show how profile properties, @Profile beans, @TestConfiguration and Testcontainers fit together, and which approach to prefer for each need.
A profile is a named set of configuration. Spring Boot loads application.properties first, then application-{profile}.properties for each active profile, and the profile values win. You activate profiles with the spring.profiles.active property, an environment variable SPRING_PROFILES_ACTIVE, a command-line argument, or, in tests, the @ActiveProfiles annotation.
Only the main file and the files for active profiles are read, so test-only settings never leak into production as long as you do not activate the profile there.
Put the file under src/test/resources. If a file with the same name also exists in src/main/resources, the test classpath version shadows it, so give test settings their own file or use different names.
The same file in YAML form is application-test.yml. A single multi-document application.yml can also contain profile sections using spring.config.activate.on-profile: test.
@ActiveProfiles replaces any profile set through spring.profiles.active for that test class. You can also pass several profiles, for example @ActiveProfiles({"test", "mock-mail"}). For a single property, @SpringBootTest(properties = "app.mail.enabled=false") is lighter than a new profile.
Use @Profile when the test needs a different implementation of a bean, such as a fake payment gateway.
@Profile("!test") means "every profile except test". Avoid scattering profile checks through production code. Prefer a small number of configuration classes, because many @Profile branches make behaviour hard to follow.
A @TestConfiguration class adds or overrides beans for tests without touching production classes. It is not picked up by component scanning, so you import it explicitly:
To replace an existing bean with a mock, use @MockitoBean (Spring Framework 6.2 and later), which supersedes the deprecated @MockBean.
An in-memory H2 database in PostgreSQL mode behaves differently from PostgreSQL for many features, such as JSON columns, specific functions and locking. For database code, run tests against the real engine:
@ServiceConnection (Spring Boot 3.1 and later) wires the datasource properties from the container, so you no longer need @DynamicPropertySource for common services. Keep test-specific settings such as disabled mail in the test profile and let the container supply the database.
| Need | Best tool |
|---|---|
| Change a few properties for one test class | @SpringBootTest(properties = ...) |
| Shared test settings for many tests | application-test.properties with @ActiveProfiles |
| Replace one bean with a fake | @TestConfiguration or @MockitoBean |
| Different implementations per environment | @Profile on configuration classes |
| Realistic database behaviour | Testcontainers with @ServiceConnection |
In src/test/resources for test-only settings. It is loaded whenever the test profile is active.
Yes. Set SPRING_PROFILES_ACTIVE=test in the environment or pass -Dspring.profiles.active=test to the test JVM, but @ActiveProfiles in code is more explicit.
Typical causes are a misspelled file name, the profile not being activated, or a property set in a higher-priority source such as an environment variable or command-line argument.
Use H2 only for simple CRUD tests. Use Testcontainers when you rely on database-specific behaviour or migrations.
Create application-test.properties, activate it with @ActiveProfiles("test"), and use @Profile, @TestConfiguration or @MockitoBean to swap beans. Prefer targeted property overrides over many profiles, and run database tests against a real PostgreSQL container when behaviour must match production.