Sub topic 15
Testing with Profiles and Property Overrides
This chapter assumes you can write a basic @SpringBootTest, you know how application.yaml and @ConfigurationProperties work, and you have seen profiles. It uses the MailProperties record and the Notifier beans from earlier chapters.
Why tests need their own configuration
A test that reads your real application.yaml gets the local-development defaults. Production values should never be reachable from a test. Spring's test support lets you pick a profile and replace single properties for one test class, without editing any file.
Activating a profile with @ActiveProfiles
@ActiveProfiles tells the test context which profiles are active. Everything profile-related then behaves as it would in a running application. Spring loads the profile's document from multi-document YAML or its application-{profile} file, and it registers @Profile beans for that profile.
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest
@ActiveProfiles("prod")
class ProdProfileTest {
@Autowired
Environment environment;
@Autowired
Notifier notifier;
@Test
void prodProfileIsActive() {
assertThat(environment.getActiveProfiles()).contains("prod");
assertThat(notifier).isNotNull();
}
}With prod active, emailNotifier is the only Notifier bean. Without @ActiveProfiles, the same test would receive loggingNotifier.
Overriding single properties
The properties attribute of @SpringBootTest sets inline key=value pairs. These sit above config files, environment variables and system properties in the precedence order, so they always win over application.yaml.
import static org.assertj.core.api.Assertions.assertThat;
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;
@SpringBootTest(properties = "app.mail.port=2525")
@ActiveProfiles("prod")
class MailPropertiesTest {
@Autowired
MailProperties mail;
@Test
void usesProdHostAndTestPort() {
assertThat(mail.host()).isEqualTo("smtp.example.com");
assertThat(mail.port()).isEqualTo(2525);
}
}The host comes from the prod document. The port comes from the test, because the inline value beats the 587 in the file.
| Mechanism | Scope | Precedence |
|---|---|---|
| `application.yaml` | Whole application | Lowest of these |
| Environment variables | Process | Above config files |
| `@SpringBootTest(properties = ...)` | One test class | Above environment variables |
| `@TestPropertySource` | One test class | Above the `properties` attribute |
Common errors
- Forgetting that validation still runs.
MailPropertiesis@Validated. If a test override setsapp.mail.port=0, the context fails to start, and every test in the class fails with a binding error. This is the intended fail-fast behavior. - Expecting a profile bean without the profile. If a test injects the email
Notifierbut no profile is active, Spring supplies the logging one. Check@ActiveProfilesfirst. - Many context variations. Each distinct combination of profiles and properties makes Spring build a new application context, and contexts are cached per combination. Many different overrides slow the test suite down.
Exercise
A test class is annotated with @SpringBootTest(properties = "app.mail.host=test.local") and @ActiveProfiles("prod"). What does mail.host() return, and why?
Show answer
It returns "test.local". The prod profile document sets the host to smtp.example.com, but the inline test property has higher precedence than config files, so it wins.
Exercise
Why does a test with @ActiveProfiles("prod") receive a different Notifier bean than a test with no profile?
Show answer
@ActiveProfiles activates prod in the test context. The @Profile("prod") emailNotifier bean is registered, and the @Profile("!prod") loggingNotifier bean is skipped.