Sub topic 5
Profiles, test properties, and @TestConfiguration
This chapter assumes you can write a @SpringBootTest test and know how Spring beans, application.properties, and profiles work. It shows three ways to change configuration for a test: properties, profiles, and extra beans.
Why tests need their own configuration
Production settings often don't suit tests. They may point at a real database, call a payment service, or use long timeouts. A test should change only what it needs and leave the rest of the application as it runs in production.
Spring Boot gives you three tools for this:
| Tool | Changes | Scope |
|---|---|---|
| `properties` attribute or `@TestPropertySource` | Individual property values | One test class |
| `@ActiveProfiles` | Which profile-specific files and beans are active | One test class |
| `@TestConfiguration` | Beans in the context | One test class or all tests that import it |
Test properties
The quickest override is the properties attribute of @SpringBootTest. Values set this way take precedence over application.properties.
@SpringBootTest(properties = {
"greeting.default-name=Test User",
"greeting.cache-enabled=false"
})
class GreetingPropertiesTest {
@Value("${greeting.default-name}")
String defaultName;
@Test
void usesOverriddenProperty() {
assertThat(defaultName).isEqualTo("Test User");
}
}For many properties, use @TestPropertySource with a file kept under src/test/resources:
@SpringBootTest
@TestPropertySource(locations = "classpath:greeting-test.properties")
class GreetingFileTest {
// ...
}Spring Boot also loads src/test/resources/application.properties instead of the one in src/main/resources, because test resources come first on the classpath. Both files have the same name, so the test copy replaces the main copy completely. It does not merge with it. To override only a few values, prefer properties or @TestPropertySource.
Profiles
A profile is a named set of configuration. With @ActiveProfiles("test"), Spring Boot also loads application-test.properties and activates beans marked @Profile("test").
@SpringBootTest
@ActiveProfiles("test")
class GreetingProfileTest {
@Autowired
Environment environment;
@Test
void testProfileIsActive() {
assertThat(environment.getActiveProfiles()).containsExactly("test");
}
}Settings in application-test.properties override those in application.properties. Settings from @TestPropertySource and properties override both.
Replacing beans with @TestConfiguration
@TestConfiguration declares extra or replacement beans for tests. Unlike @Configuration, component scanning does not pick it up automatically. You add it explicitly with @Import.
@TestConfiguration
class FixedClockConfig {
@Bean
Clock clock() {
return Clock.fixed(Instant.parse("2026-01-01T10:00:00Z"), ZoneOffset.UTC);
}
}@SpringBootTest
@Import(FixedClockConfig.class)
class ClockTest {
@Autowired
Clock clock;
@Test
void usesFixedTime() {
assertThat(clock.instant()).isEqualTo(Instant.parse("2026-01-01T10:00:00Z"));
}
}If the application already defines a bean named clock, the test bean does not replace it by default. Spring Boot disables bean definition overriding, so startup fails. Give the test bean a different name and mark it @Primary, or mock the existing bean as the previous chapter's approach describes.
Common errors
- Putting
@TestConfigurationon a class that you expect to apply automatically. It applies only to tests that import it, or when it is a static nested class of the test. - Creating
src/test/resources/application.propertiesand losing every setting from the main file. - Forgetting
@ActiveProfiles, soapplication-test.propertiesis silently ignored. - Defining a bean with the same name as an existing one and hitting a bean overriding error.
Different properties, profiles, or imports give a test a different context configuration. Spring then builds a new context for it instead of reusing a cached one, so each variation costs startup time.
Exercise
A test class has @SpringBootTest(properties = "app.mode=fast") and @ActiveProfiles("test"). Both application-test.properties and application.properties set app.mode=slow. Which value does the test see, and why?
Show answer
It sees fast. Properties set in the @SpringBootTest properties attribute have higher precedence than any properties file, including the profile-specific application-test.properties.
Exercise
You write a top-level @TestConfiguration class that defines a mock PaymentClient bean, but your test still uses the real one. What is the likely cause and fix?
Show answer
The test class does not import the configuration. Add @Import(YourTestConfig.class) to the test. If the application already defines a bean with the same name, rename the test bean and mark it @Primary.