Test Fixtures (JVM)¶
Share generated fakes across JVM modules using Gradle’s built-in test fixtures plugin — no collector module needed.
JVM Only
This feature requires the java-test-fixtures Gradle plugin, which is only available for JVM and Android projects. For KMP multi-module projects, use the Collector Module approach instead.
Test Fixtures vs Collector Module¶
| Test Fixtures | Collector Module | |
|---|---|---|
| Platform | JVM / Android only | KMP + JVM + Android |
| Extra module needed? | No | Yes (:module-fakes) |
| Setup complexity | Minimal (2 lines) | Moderate (new module + config) |
| Consumer dependency | testFixtures(projects.core) |
implementation(projects.core.fakes) |
| Best for | JVM multi-module projects | KMP projects, Maven publishing |
Setup¶
Step 1: Configure the Producer Module¶
Apply the java-test-fixtures plugin and enable useGradleTestFixtures:
// core/build.gradle.kts
plugins {
kotlin("jvm")
`java-test-fixtures`
alias(libs.plugins.fakt)
}
fakt {
useGradleTestFixtures.set(true)
}
Define your @Fake annotated types as usual:
// core/src/main/kotlin/com/example/core/UserRepository.kt
@Fake
interface UserRepository {
fun findById(id: String): User?
fun save(user: User): User
fun delete(id: String): Boolean
}
Build the module to generate fakes:
Verify fakes are generated in the testFixtures directory:
Step 2: Consume in Another Module¶
In the consumer module, add a testFixtures() dependency — no Fakt plugin needed:
// app/build.gradle.kts
plugins {
kotlin("jvm")
}
dependencies {
implementation(projects.core)
testImplementation(testFixtures(projects.core))
}
Use the generated fakes in tests:
// app/src/test/kotlin/com/example/app/UserServiceTest.kt
class UserServiceTest {
@Test
fun `GIVEN user exists WHEN getUser THEN returns user`() {
val repo = fakeUserRepository {
findById { id -> User(id, "Alice") }
}
val service = UserService(repo)
val result = service.getUser("1")
assertEquals("Alice", result?.name)
}
}
How It Works¶
- Fakt detects
java-test-fixturesplugin anduseGradleTestFixtures.set(true) - Generated fakes go to
build/generated/fakt/testFixtures/kotlin/instead oftest/ - The
testFixturessource set is configured to include the generated directory - The module’s own tests can use the fakes (Gradle makes testFixtures visible to test automatically)
- Other modules import fakes via
testFixtures(project(":core"))— standard Gradle mechanism
Validation¶
If useGradleTestFixtures is enabled but the java-test-fixtures plugin is not applied, Fakt emits a warning and falls back to the default test source set:
Fakt: useGradleTestFixtures is enabled but the 'java-test-fixtures' plugin
is not applied. Add `java-test-fixtures` to your plugins block:
plugins {
`java-test-fixtures`
}
Falling back to default 'test' source set.
Sample Project¶
See samples/jvm-test-fixtures/ for a working multi-module example with:
core/— Producer with interface, abstract class, and open classapp/— Consumer using fakes from core’s testFixtures
Next Steps¶
- Multi-Module (KMP) — Collector module approach for KMP projects
- Plugin Configuration — All configuration options
- Samples — All sample projects