/kotlin-ksp-mocks:kotlin-ksp-mocksfrom kotlin-ksp-mocks
Generate recording-spy mocks for Kotlin interfaces with the dev.modaal mocks-processor KSP processor. Use when asked to add mock generation to a Gradle module, mock a Kotlin interface for a test, replace a hand-written fake, wire kspTest or kspJvmTest, set kspMocksTargets, or print or read the members of a generated <Interface>Mock — and when diagnosing "Handler expected to be set.", a test that hangs collecting a mock's Flow, "is not resolvable in this compilation", "declares type parameters", or a missing <fn>Args record. Covers Kotlin/JVM, Kotlin Multiplatform, Android and testFixtures wiring. Generation runs in the test compilation only, so no annotation reaches the main classpath and no generated file is committed.
Ships in kotlin-ksp-mocks. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
A KSP processor that writes one <Interface>Mock per interface named in a build-script list — not
by an annotation. The mock records calls, seeds returns through handlers, and is generated into the
test compilation of the module that wires it. Nothing is committed and nothing lands on the main
classpath.
| the module | the dependency line | where the mock lands |
|---|---|---|
| Kotlin/JVM | kspTest("dev.modaal:mocks-processor:<version>") | build/generated/ksp/test/kotlin/ |
| Kotlin Multiplatform, JVM tests | dependencies { add("kspJvmTest", "dev.modaal:mocks-processor:<version>") } — the typed accessor kspJvmTest(…) does not exist and the build script will not compile with it | build/generated/ksp/jvm/jvmTest/kotlin/ |
| Android, AGP 9 | dependencies { add("kspTest", …) }, and do not apply kotlin("android") — AGP 9 refuses it | build/generated/ksp/debugUnitTest/kotlin/ |
| Android, AGP 8 | dependencies { add("kspTest", …) }, with kotlin("android") applied | build/generated/ksp/debugUnitTest/kotlin/ |
| the interfaces live in another module | wire KSP in the module that holds the tests and list the other module's interfaces there. That module must be on this one's test compile classpath, which implementation(project(":core")) already gives | the test module's own build/, in the interface's package |
| several modules' tests need the same mocks | dependencies { add("kspTestFixtures", …) } in the module that owns the interfaces, plus at least one Kotlin file under src/testFixtures/kotlin/, then testImplementation(testFixtures(project(":core"))) in each consumer | build/generated/ksp/testFixtures/kotlin/ |
A multiplatform module's mock is visible to the platform test source set only. A commonTest source
set cannot see a mock generated for the JVM test compilation.
When the module shape is not known, one command finds what was written, whichever row applies:
find . -path '*/build/generated/ksp/*' -name '<Interface>Mock.kt'
It comes back empty when KSP did not run for that source set, which
./gradlew :<module>:kspTestKotlin --info reports.
Three edits. First, the repository that serves the artifact — it is on neither Maven Central nor Google's repository:
// settings.gradle.kts
dependencyResolutionManagement {
repositories {
mavenCentral()
maven { url = uri("https://modaal-agent.github.io/maven") }
}
}
Then the KSP plugin, the processor on the test configuration the table above names, and the target list:
// build.gradle.kts of the module whose tests need mocks
plugins {
kotlin("jvm")
id("com.google.devtools.ksp") version "<ksp version>"
}
dependencies {
testImplementation(kotlin("test"))
kspTest("dev.modaal:mocks-processor:<version>")
}
ksp {
arg("kspMocksTargets", "com.example.FeedEnvironment,com.example.FeedDependency")
}
kspMocksTargets is a comma-separated list of fully-qualified interface names. A nested interface is
written with dots — com.example.Outer.Inner.
Resolve <version> rather than pinning a number from memory. The host publishes a Maven metadata
file whose <release> element is the newest published version:
curl -s https://modaal-agent.github.io/maven/dev/modaal/mocks-processor/maven-metadata.xml
<ksp version> is a KSP release that supports the project's Kotlin version. The full wiring
reference, including what to do when the compile worker runs an old JVM, is
references/gradle-wiring.md.
The mock is a class named <Interface>Mock in the interface's package, so a test in another
package imports it.
interface FeedEnvironment {
suspend fun load(id: String): FeedPage
fun log(message: String)
var volume: Double
}
val environment = FeedEnvironmentMock()
environment.loadHandler = { id -> FeedPage(id) }
environment.load("p1")
environment.loadCallCount // 1
environment.loadArgs // ["p1"]
| member | what it holds |
|---|---|
<fn>CallCount | how many times the requirement was called, counted before the handler runs |
<fn>Args | one entry per call, in order: a single recordable parameter directly, two or more as a nested <Fn>Args data class |
<fn>Handler | the nullable lambda that decides the return value and the side effects — the only seeding mechanism, suspend carried into it |
<fn>Channel | for a function returning Flow — what the mock replays while <fn>Handler is unset |
<prop>GetCount, <prop>GetHandler | reads of a property requirement, and the lambda that decides what a read returns |
<prop>SetCount, <prop>SetArgs, <prop>SetHandler | writes to a var requirement: counted, each value recorded, then handed to the lambda after the store is assigned. Construction does not count, and a function-typed property has no <prop>SetArgs |
_<prop> | the store behind a property: seed it and read it without moving a counter |
<prop>Channel | for a read-only Flow property — what the mock replays while <prop>GetHandler is unset |
<fn>SubscribeCount, <fn>SubscribeCancelCount | on a channel-backed Flow member: collections started, and collections that stopped early |
<fn>OutputCount, <fn>Outputs, <fn>OutputHandler | values delivered: counted, recorded in order, handed to the lambda after the record |
<fn>CompletionCount | the stream ended or failed; a cancelled collection counts in <fn>SubscribeCancelCount |
Without a handler, in order: a Unit function returns nothing; a Flow replays <fn>Channel;
a nullable return gives null; a guessable default (0, false, "", emptyList(), …) is
returned; anything else fails with IllegalStateException carrying
"<fn>Handler expected to be set.".
Properties. Seed a read-only one with mock._<prop> = value — mock.<prop> = value does not
compile — and give the constructor one with no guessable default,
FeedDependencyMock(config = config). A read-only Flow property has no store; <prop>Channel is its
fallback.
Only an exact Flow<E> property is channel-backed. A StateFlow, a SharedFlow and a sink-shaped
requirement (FlowCollector, SendChannel) are ordinary properties, so a read-only one is
constructor-seeded — give the constructor a MutableStateFlow or a Channel and drive it.
INIT="${TMPDIR:-/tmp}/print-mock-api.init.gradle.kts"
curl -fsSL -o "$INIT" \
"https://raw.githubusercontent.com/modaal-agent/kotlin-ksp-mocks/<version>/gradle/print-mock-api.init.gradle.kts"
./gradlew -I "$INIT" :<module>:printMockApi -q
<version> is the mocks-processor version the module declares; each release tag publishes the
Gradle init script at that path. For each interface in the module's kspMocksTargets the task prints
// <fqn> and the whole generated <Interface>Mock.kt the test compilation writes. It edits no build
file, compiles nothing of the module, and runs while main does not compile; add
-PmockApiTargets=<fqn>,<fqn> to print interfaces not listed yet. It covers Kotlin/JVM,
multiplatform and testFixtures modules — a 404, the sandbox's hosts, module shapes, Android and
failures are in references/printing-members.md.
Push, then close to end the stream. A collector waits on an open channel forever, so the close is what makes the test terminate.
@Test
fun `events reach the collector`() = runTest {
val environment = FeedEnvironmentMock()
environment.eventsChannel.trySend(FeedEvent.Tick)
environment.eventsChannel.close()
assertEquals(listOf(FeedEvent.Tick), environment.events().toList())
assertEquals(1, environment.eventsCallCount)
assertEquals(1, environment.eventsSubscribeCount) // something collected it
assertEquals(listOf(FeedEvent.Tick), environment.eventsOutputs)
assertEquals(1, environment.eventsCompletionCount)
}
<fn>Outputs holds the delivered values, so a test asserts on what crossed the member without
writing a collector of its own. Four rules:
first(), take(n) and a timeout each
leave <fn>SubscribeCancelCount at 1 and <fn>CompletionCount at 0; a stream that fails counts
<fn>CompletionCount, as a normal end does.SharedFlow from the handler to give both the same ones.eventsHandler = { flowOf(FeedEvent.Tick) } — and its stream is counted the same way.vararg parameters fail generation with an error naming the member.
Wrap the use site in a non-generic interface, or take a List instead of a vararg.kspMocksTargets fails generation.<fn>Args — storing a closure
would pin the caller's captures to the mock's lifetime. Assert on those through the handler.update(id, force) beside update(id) gives
updateIdForceCallCount.| symptom | what to do |
|---|---|
Could not find dev.modaal:mocks-processor | add the maven { … } line to settings.gradle.kts |
kspMocksTargets: <fqn> is not resolvable in this compilation | put the declaring module on the test compile classpath; write a nested interface as Outer.Inner |
kspMocksTargets: <fqn> is not an interface | interfaces only — not a class, object or data class |
kspMocksTargets: <fqn> declares type parameters — generic interfaces are not supported | wrap the use site in a non-generic interface, or hand-write that double |
kspMocksTargets: <fqn>.<fn> has a vararg parameter — not supported | take a List, or hand-write that double |
kspMocksTargets: <fqn> — <prop> is generated twice | two requirements make one mock member, and the line names both: rename one |
Unresolved reference on <Interface>Mock | check the module table, then kspMocksTargets, then the import — the mock is in the interface's package |
IllegalStateException with <fn>Handler expected to be set. | set <fn>Handler; the return type has no guessable default |
a test collecting a mock's Flow never finishes — UncompletedCoroutinesError under runTest | close() the channel after the sends, or seed the handler |
<fn>CompletionCount or <fn>OutputCount stays 0 | the collector stopped early — assert <fn>SubscribeCancelCount; <fn>SubscribeCount says whether anything collected |
<fn>Args does not exist | every parameter is function-typed, or there are none: assert through <fn>Handler and <fn>CallCount |
| the mock constructor demands an argument | a read-only requirement with no guessable default is constructor-seeded — give it to the constructor |
UnsupportedClassVersionError naming KspMocksProcessorProvider | point the build daemon at JDK 17 or newer: gradle/gradle-daemon-jvm.properties, toolchainVersion=<major> |
Each in full, with the text to match: references/troubleshooting.md.
printMockApi for each module
shape, what it reads from the build, and its failure lines.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub modaal-agent/kotlin-ksp-mocks --plugin kotlin-ksp-mocks