Fake is a Gradle plugin powered by KSP that automatically generates type-safe fake builders for Kotlin
data class models —
designed for tests only to generate fakes for models while testing.
It works with Kotlin, Android, and Kotlin Multiplatform (KMP) projects.
Given model:
data class User(
val id: String,
val age: Int,
val tags: List<String>
)Fake generates:
val user = fakeUser {
id = "123"
age = 30
}plugins {
id("io.github.mohaberabi.fakeklass.plugin") version "<version>"
}
dependencies {
add("kspCommonMainMetadata", "io.github.mohaberabi:fake:<version>")
}Make sure mavenCentral() is configured.
Tell FakeKlass which packages contain your models: Generated fakes are available and shipped **only in for the source set you want **.
ksp {
arg("fake.packages",
"com.example.models",
"com.example.domain")
}
sourceSets {
commonTest {
kotlin.srcDir(layout.buildDirectory.dir("generated/ksp/metadata/commonMain"))
}
}
@Test
fun createUser() {
val user = fakeUser {
age = 42
}
assertEquals(42, user.age)
}