Affected Tests Docs

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.

TriggerSettingDefault
A class annotated @SpringBootApplicationonApplicationClassChangedfull_suite
A class annotated @Configuration, @SpringBootConfiguration or @ConfigurationPropertiesonConfigurationClassChangedfull_suite

Each setting takes one of three values:

ValueEffect
full_suiteRun every test. This is the safe default.
integration_testsAdd every integration test (the classes matching integrationTestPatterns) to whatever discovery selected.
offTreat 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.Configuration and Configuration are equivalent. The list settings applicationAnnotations, configurationAnnotations and integrationTestPatterns redefine what counts as a trigger or an integration test. An empty list keeps the default; set the matching onXxx to off to disable a trigger.
  • Integration tests are found by name pattern. integrationTestPatterns are globs over the test class name with dots read as /. The defaults are **/*IT, **/*ITTest and **/*IntegrationTest. If you pick integration_tests and no test matches, the plugin falls back to a full suite instead of silently adding nothing.
  • integration_tests in CI. When discovery finds no unit tests, the integration tests are added to that empty selection, unless the mode's DISCOVERY_EMPTY action is a full suite, which is true for ci and strict. There the full suite still runs. A Spring trigger that adds integration tests also overrides a skipped outcome 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 integrationTestPatterns entry fails the build, even when no trigger fires, and names its index and pattern.
  • Kotlin. With kotlinEnabled = false no .kt file is parsed, so Kotlin configuration classes never trigger.
  • See the decision. --explain prints a Spring escalation: block naming each file, its annotation and the setting that decided the outcome. The --explain page has the JSON form.

Other Spring changes are already covered

  • application.yml and application.properties changes 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 @Profile or @Primary implementation, for instance) is covered by the headerEdges strategy, 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 @Bean methods into a @Configuration class, 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.