The Java Test Fixtures plugin (plugin id: java-test-fixtures) adds support for producing and consuming test fixtures — shared test helper code such as fake implementations, test data builders, or base classes that are used by the project’s own tests and can also be consumed by other projects.

Despite the name, fixtures may be written in any supported JVM language, not only Java.

For a task-oriented guide — producing, consuming, and publishing fixtures — see Test fixtures in Testing in Java & JVM projects.

Usage

Apply the plugin alongside a JVM library or application plugin:

build.gradle.kts
plugins {
    `java-library`
    `java-test-fixtures`
}
build.gradle
plugins {
    id 'java-library'
    id 'java-test-fixtures'
}

Applying the plugin (it applies the Java (base) plugin automatically):

  • creates a testFixtures source set and a corresponding feature exposing a <group>:<name>-test-fixtures capability;

  • makes the fixtures depend on the main component, and the project’s own test suite depend on the fixtures — both by convention, so no wiring is required;

  • when the Java Plugin is applied, exposes the fixtures to consumers as additional variants of the project’s component.

Project layout

src/testFixtures/java

Test fixture source files.

src/testFixtures/resources

Test fixture resources.

Dependency configurations

The plugin adds a set of configurations for the testFixtures source set, mirroring those of a normal Java library:

testFixturesApi

Declarable. Dependencies exported to consumers of the fixtures (and to the fixtures' own compilation).

testFixturesImplementation

Declarable. Dependencies used internally by the fixtures and not exposed to consumers.

testFixturesCompileOnly / testFixturesCompileOnlyApi

Declarable. Compile-time-only dependencies of the fixtures (the Api variant is also visible to consumers at compile time).

testFixturesRuntimeOnly

Declarable. Runtime-only dependencies of the fixtures.

testFixturesApiElements / testFixturesRuntimeElements

Consumable. The variants exposed to consumers for compiling against, and running against, the fixtures.

testFixturesCompileClasspath / testFixturesRuntimeClasspath

Resolvable. The compile and runtime classpaths used to build the fixtures.

Consuming test fixtures

Reference the fixtures of another project (or an external module) with the DependencyHandler.testFixtures(Object) method:

build.gradle.kts
dependencies {
    testImplementation(testFixtures(project(":lib")))
}
build.gradle
dependencies {
    testImplementation(testFixtures(project(":lib")))
}

The same method accepts standard module notation — testFixtures("com.example:lib:1.0") — for fixtures published to a repository. See Consuming test fixtures and Publishing test fixtures for details.