Spring and @Bean changes
A common question about test selection is: if I change how a Spring bean is built, do the tests that depend on that bean run? This page explains the problem, what the plugin does about it, and where the limits are.
The problem: wiring that no source file mentions
Spring connects objects at runtime. Take a rate limiter that an email sender needs.
@Configuration
public class NotificationsRateLimitConfig {
@Bean
public RateLimiterFactory rateLimiterFactory() {
return new RateLimiterFactory();
}
@Bean
public RateLimiter emailRateLimiter(RateLimiterFactory factory) {
return factory.create("email", 60);
}
}
@Component
public class EmailSender {
private final RateLimiter rateLimiter;
public EmailSender(RateLimiter rateLimiter) {
this.rateLimiter = rateLimiter;
}
}
EmailSender asks for a RateLimiter in its constructor and never says where it comes from. Spring finds the emailRateLimiter method and supplies the value. This is on purpose: the sender does not know how a limiter is built, so you can change the limit, swap the implementation or mock it in a test without touching the sender.
Now someone changes emailRateLimiter from 60 permits a minute to 1. An integration test such as EmailSenderIT starts the Spring context and would behave differently. But no file in the project names NotificationsRateLimitConfig: not the sender, not the test. Every source-level strategy (naming, usage, impl, transitive) works by following names and imports, so none of them can see the link, and the test is not selected.
You can reproduce this in the playground: choose Change a @Bean method. It changes NotificationsRateLimitConfig. The project contains EmailSender and EmailSenderIT, and nothing refers to the config class by name.
What the plugin does
When a changed production file declares a class carrying one of the configured annotations, the plugin widens the selection.
| Trigger | Setting | Default |
|---|---|---|
A class annotated @SpringBootApplication | onApplicationClassChanged | full_suite |
A class annotated @Configuration, @SpringBootConfiguration or @ConfigurationProperties | onConfigurationClassChanged | full_suite |
Each setting takes one of three values:
| Value | Effect |
|---|---|
full_suite | Run every test. This is the safe default. |
integration_tests | Add every integration test (the classes matching integrationTestPatterns) to whatever discovery selected. |
off | Treat the class like any other and rely on the normal strategies. |
For the example above, with the defaults, changing NotificationsRateLimitConfig reports SPRING_CONFIG_CHANGED and runs the full suite. With off, the plugin finds nothing (no test names the class), reports DISCOVERY_EMPTY, and skips locally or runs everything in CI. With integration_tests it runs the unit tests discovery found plus every integration test, including EmailSenderIT.
Narrowing the default
A full suite on every configuration change is correct but expensive. Most teams want the middle option:
affectedTests {
onConfigurationClassChanged = "integration_tests"
}
Bean wiring can only change what a Spring context does, and only integration tests start a context. Unit tests that call new EmailSender(...) are already selected by the normal strategies when the sender itself changes.
Details worth knowing
- Matching is by simple name.
org.springframework.context.annotation.ConfigurationandConfigurationare equivalent. The list settingsapplicationAnnotations,configurationAnnotationsandintegrationTestPatternsredefine what counts as a trigger or an integration test. An empty list keeps the default; set the matchingonXxxtooffto disable a trigger. - Integration tests are found by name pattern.
integrationTestPatternsare globs over the test class name with dots read as/. The defaults are**/*IT,**/*ITTestand**/*IntegrationTest. If you pickintegration_testsand no test matches, the plugin falls back to a full suite instead of silently adding nothing. integration_testsin CI. When discovery finds no unit tests, the integration tests are added to that empty selection, unless the mode'sDISCOVERY_EMPTYaction is a full suite, which is true forciandstrict. There the full suite still runs. A Spring trigger that adds integration tests also overrides askippedoutcome for an empty or incomplete discovery, so the integration tests still run.- Any edit counts. A comment-only change to a file that declares such a class still triggers, because the plugin does not inspect which lines changed.
- Deleted classes do not trigger. A deleted configuration class cannot be read.
- Bad patterns fail loudly. A malformed
integrationTestPatternsentry fails the build, even when no trigger fires, and names its index and pattern. - Kotlin. With
kotlinEnabled = falseno.ktfile is parsed, so Kotlin configuration classes never trigger. - See the decision.
--explainprints aSpring escalation:block naming each file, its annotation and the setting that decided the outcome. The--explainpage has the JSON form.
Other Spring changes are already covered
application.ymlandapplication.propertieschanges are unmapped files, so they run the full suite by default. They are not part of this feature.- A changed implementation that tests only reach through its interface (a
@Profileor@Primaryimplementation, for instance) is covered by theheaderEdgesstrategy, which adds the interface and its contract tests. See how it works.
What is not covered
The trigger is a class-level annotation, so a @Bean method is only noticed when its class carries one of the configured annotations. A @Bean method inside a class that is not @Configuration (for example a @Component) is not a trigger. If nothing else names that class, changing it selects no tests through the normal strategies: DISCOVERY_EMPTY, which is a skip locally and a full suite in CI.
You cannot fix that by adding Bean to configurationAnnotations, because @Bean is a method annotation. Adding Component would work but is usually too wide, since every service class would then escalate. The options are:
- Move the
@Beanmethods into a@Configurationclass, which is the idiomatic place for them anyway. - If the classes that declare beans share a marker annotation of your own, add it to
configurationAnnotations.