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: the fixtures get their own api/implementation/compileOnly/compileOnlyApi/runtimeOnly declarable configurations, their own consumable elements, and their own resolvable classpaths.

Applying this plugin also wires up two dependencies for you automatically:

  • testFixturesApi gets an implicit dependency on the project itself, so the fixtures can use the project’s own main classes.

  • The test source set’s testImplementation configuration (added by the Java Plugin) gets an implicit testFixtures(project(…​)) dependency on this project, so your tests can use the fixtures without declaring anything yourself.

Declarable (Bucket) Configurations

Configuration name Role Description

testFixturesApi

Declarable

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

testFixturesImplementation (extends testFixturesApi)

Declarable

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

testFixturesCompileOnly (extends testFixturesCompileOnlyApi)

Declarable

Compile-time-only dependencies of the fixtures, not exposed to consumers or at runtime.

testFixturesCompileOnlyApi

Declarable

Compile-time-only dependencies of the fixtures that are also visible to consumers at compile time.

testFixturesRuntimeOnly

Declarable

Runtime-only dependencies of the fixtures.

Consumable Configurations

Configuration name Role Description

testFixturesApiElements (extends testFixturesApi, testFixturesCompileOnlyApi)

Consumable

The elements exposed to consumers that compile against the fixtures.

testFixturesRuntimeElements (extends testFixturesImplementation, testFixturesRuntimeOnly)

Consumable

The elements exposed to consumers that run against the fixtures.

Resolvable Configurations

Configuration name Role Description

testFixturesCompileClasspath (extends testFixturesCompileOnly, testFixturesImplementation)

Resolvable

The classpath used when compiling the fixtures.

testFixturesRuntimeClasspath (extends testFixturesRuntimeOnly, testFixturesImplementation)

Resolvable

The classpath used to run 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.