Affected Tests Docs

How it works

The pipeline has five stages:

  1. Detect what changed, by diffing HEAD against the base ref with JGit. No git binary is needed.
  2. Bucket each path as ignored, out-of-scope, production, test or unmapped.
  3. Resolve the situation, such as UNMAPPED_FILE or DISCOVERY_SUCCESS.
  4. Discover the tests that the changed Java and Kotlin classes can affect.
  5. Execute the subset, the full suite, or nothing, depending on the action the situation maps to.
Flowchart: git diff feeds PathToClassMapper, which buckets each changed file as ignored, out-of-scope, production, test or unmapped. The bucket counts decide a situation: EMPTY_DIFF, ALL_FILES_IGNORED, ALL_FILES_OUT_OF_SCOPE, UNMAPPED_FILE, SPRING_CONFIG_CHANGED, DISCOVERY_INCOMPLETE, DISCOVERY_EMPTY or DISCOVERY_SUCCESS. The situation maps to an action (SELECTED, FULL_SUITE or SKIPPED), resolved from an explicit onXxx setting first and the mode default second.
From git diff to dispatched tests

Discovery strategies

All five strategies run against every changed production class, and their results are merged. A test runs if any strategy identifies it. The goal is full coverage of what a change can affect: running a few extra tests is always better than missing one.

StrategyWhat it doesExample
namingFor a changed class Foo, finds FooTest, FooIT, FooITTest and FooIntegrationTest (the suffixes are configurable). It only looks at file names, so no parsing is needed. The match ignores packages on purpose, so tests kept in a different package from the code under test are not missed. The cost is over-selection when two production classes in different packages share a simple name. --explain prints a hint listing those pairs.FooService changed, so FooServiceTest and FooServiceIT run.
usageParses every test file with JavaParser and checks whether it references a changed class. Tier one is a direct import. Tier two is a type-reference scan for wildcard imports and same-package usage, which catches fields, parameters, return types, constructor calls, generics and casts.Receipt changed, so ReceiptValidatorTest (imports it) and ReceiptMapperTest (same package, uses it as a field type) run.
implWhen an interface or base class changes, finds the classes that extends or implements it, plus classes named *Impl or Default*, then runs naming and usage on those.FooService changed, so DefaultFooService and DefaultFooServiceTest run.
transitiveBuilds a reverse dependency map of production classes (which classes use which, through field types) and walks it up to transitiveDepth levels (default 4, maximum 5), then runs naming and usage on the consumers.PaymentGateway changed, FooService uses it (depth 1), OrdersController uses FooService (depth 2), and both tests run.
headerEdgesCloses the Spring dependency-injection gap. It reads each changed class's header (everything before the opening {) and adds the types it names, before the other strategies run. Six categories are followed: extends, implements, permits, generic type bounds, record components and class-level annotations. Three safety layers keep it from exploding: default ignore globs for JDK, Spring, JUnit, Mockito, Kotlin and Lombok types, a depth bound (default 1, maximum 2), and a sibling cap (default 5) for over-connected base classes.StripeGateway implements PaymentGateway changed, so PaymentGateway joins the changed set and its contract test PaymentGatewayTest runs, even though no source file imports StripeGateway.

Any test file that is itself modified in the diff always runs, whatever the strategies found.

Strategies run in parallel on a small thread pool, and collapse to a serial run on a single-CPU host. --explain prints how long each one took.

How scanning works

The plugin scans the project tree recursively at any depth to find source and test directories, so it makes no assumption about module layout. Flat (api/src/test/java), nested (services/orders/src/test/java) and deeply nested layouts are all found. .git, build, .gradle and node_modules are skipped. outOfScopeTestDirs and outOfScopeSourceDirs are filtered out at index time, so discovery never picks up tests that live there.

Source-set auto-discovery

When the Java plugin (or anything built on java-base) is applied, the plugin reads each subproject's JavaPluginExtension source sets after projects are evaluated. A source set counts as a test source set when a Test-typed task points at its output, the same rule JvmTestSuitePlugin uses. Everything else goes into sourceDirs. So src/integrationTest/java with a matching integrationTest task is picked up with no configuration, and so are custom layouts such as a main source set rooted at src/cool/java.

Setting sourceDirs or testDirs explicitly always wins over the auto-discovered default. Do that on a monorepo where one module's helper source set should not influence selection.

Multi-module projects

No configuration is needed. Each discovered test class is traced back to the Gradle subproject that owns its file, and the tests are dispatched per module:

./gradlew :moduleA:test --tests com.example.FooTest \
          :moduleB:test --tests com.example.BarTest

This keeps each --tests filter inside its owning module, instead of applying it everywhere and failing on subprojects that do not contain the class. Imports across modules are still detected, for example a test in application that imports a class from api.

Persistent discovery cache

Every run saves its index (directory structure, file lists, the class-to-path map and per-file syntax data) to build/affected-tests/index/v1/snapshot.tsv. The next run on the same tree skips the scan and only re-parses files whose modified time or size changed. A change to any setting that affects selection (strategies, suffixes, depth, implementation naming, ignore paths, scan directories and the Kotlin and header-edges settings) invalidates the cache.

To force a rebuild, delete build/affected-tests/index, or disable the cache for a run with -Daffected-tests.indexCache.enabled=false.

Known limitations

  • Kotlin is parsed by default. .kt files take part in discovery the same way .java files do. Three limits apply: @file:JvmName(...) is detected and reported by --explain, but the synthetic class name still uses the <basename>Kt shape; the parser targets Kotlin/JVM and omits the JS, Native, WASM and IR subsystems to keep the plugin small; and the Kotlin compiler environment registers process-wide state on first use. If a multi-module build with org.gradle.parallel=true hits a bootstrap collision, set affectedTests { kotlinEnabled = false } to fall back to path-based mapping for .kt files. See the Kotlin design notes.
  • Other JVM languages are unmapped. .kts, .groovy and .scala changes fall into the unmapped bucket and escalate to the full suite under the default ci and strict profiles. Groovy support is tracked in #47. If your production code is Java and your tests are Groovy or Scala, add those test directories to outOfScopeTestDirs so a change there is skipped, and run them in their own Gradle task on every merge request.
  • @Bean methods outside a @Configuration class. A @Bean method declared in a class that is not annotated @Configuration (a @Component, for instance) is not detected as Spring configuration, so changing it selects tests only through the normal strategies. See Spring and @Bean changes. Changes to application.yml, application.properties and XML are not affected: they are unmapped files and run the full suite by default.
  • Git access is in-process. The diff is computed with JGit reading the working tree's .git directory. Bare repositories, alternate worktrees without their own .git/objects, and shallow clones that lack the base ref are not supported. The run fails before selection, so a pipeline never silently skips tests.