
Mocking library using the MockK DSL as a compiler plugin: compiles mocks at build time, no runtime instrumentation, compile-time errors for unmockable calls, scoped test state.
lokki is a mocking library for Kotlin/JVM with the MockK DSL, implemented as a K2 compiler
plugin. mock<T>() compiles into an ordinary class that implements T, and the blocks of every { } and
verify { } compile into calls of the runtime instead of being executed. lokki keeps the DSL of MockK, not every
behaviour of its runtime: what MockK does with a Java agent either fails compilation with an alternative or
behaves differently.
mockObject, mockStatic and stubs added to shared mocks last until their scope — a JUnit 5
test or lokkiScope { } — closes.Status: alpha. Until 1.0, the API may change without a deprecation cycle, and mocks compiled by an older lokki may be rejected by a newer runtime: recompile test fixtures and test libraries when upgrading.
mockObject and mockStatic need 2.4.20 or newer.LokkiExtension; lokki raises an older JUnit 5 to 5.12.2.plugins {
kotlin("jvm") version "2.4.20"
id("io.github.wordyarc.lokki") version "0.3.0"
}The plugin applies to test compilations — test, testFixtures, JVM test suites, jvmTest of multiplatform
projects and Android test variants — and adds the runtime, io.github.wordyarc.lokki:lokki-core, to them. The
runtime includes the JUnit 5 extension. Register it for all tests in src/test/resources/junit-platform.properties:
junit.jupiter.extensions.autodetection.enabled=true
junit.jupiter.extensions.autodetection.include=io.lokki.junit5.*Every test then runs in its own scope.
class Job(val id: String)
interface JobQueue {
fun poll(): Job?
fun complete(job: Job)
}
class JobProcessor(private val queue: JobQueue) {
fun runNext(): Boolean {
val job = queue.poll() ?: return false
queue.complete(job)
return true
}
}import io.lokki.Runs
import io.lokki.every
import io.lokki.just
import io.lokki.mock
import io.lokki.verify
import kotlin.test.Test
import kotlin.test.assertTrue
class JobProcessorTest {
@Test
fun completesPolledJob() {
val queue = mock<JobQueue>()
val job = Job("42")
every { queue.poll() } returns job
every { queue.complete(any()) } just Runs
assertTrue(JobProcessor(queue).runNext())
verify(exactly = 1) { queue.complete(job) }
}
}mock<T>() accepts interfaces, open and abstract classes and function types, including generic types and suspend
members. Matchers, answers, coEvery/coVerify, ordered verification, relaxed mocks and spies work as in MockK,
with the differences listed in Coming from MockK.
Misuse fails compilation:
mock<Job>()
// e: 'com.example.Job' cannot be mocked: it is a final class. Extract an interface,
// make the class and its members open, or add it to lokki { mockable(...) }.
every { queue.complete(any()) }
// e: 'every' registers nothing without an answer: add 'returns', 'answers', 'throws'
// or 'just Runs' after the block.A scope is the lifetime of what a test does to mocks. When it closes, the stubs and recorded calls added in the test are removed, mocked objects, functions and constructors are restored, and the test fails if a mock call had no answer, even if the code under test caught the exception.
LokkiExtension runs every test class, test and dynamic test in its own scope and initializes @Mock,
@RelaxedMock, @Spy and @InjectMocks properties:
class JobProcessorTest {
@Mock
lateinit var queue: JobQueue
@Test
fun skipsEmptyQueue() {
every { queue.poll() } returns null
assertFalse(JobProcessor(queue).runNext())
verify { queue.poll() }
confirmVerified()
}
}The properties file from Installation registers the extension for all tests, and
@ExtendWith(LokkiExtension::class) for one class. @LokkiExtension.ConfirmVerification and
@LokkiExtension.CheckUnnecessaryStub on a test class run confirmVerified() and checkUnnecessaryStub() after each
test. @LokkiExtension.KeepMocks on a test class or method keeps the stubs and recorded calls of a test for the rest
of the class.
Without JUnit 5, lokkiScope { } provides the scope and MockAnnotations.init(this) initializes the properties.
Outside a scope, mocks, stubs and verification work, but nothing is removed or checked when a test ends. Calls that
change global state or act on all mocks of a test throw LokkiException there: mockObject, mockStatic,
mockConstructor, stubs of objects and top-level functions, and confirmVerified(), checkUnnecessaryStub() and
clearAllMocks() without arguments.
Without an agent, only calls of open members can be intercepted. Declarations of the module's main code that match a
mockable pattern are compiled differently: classes and their members are open in the bytecode, and objects and
top-level functions get interception points. A pattern is a package with its subpackages (com.example.**), a class,
an annotation or a function.
// build.gradle.kts
lokki {
mockable("com.example.**")
}// src/main/kotlin
object Clock {
fun hour(): Int = 12
}// src/test/kotlin, in a scope
@Test
fun morning() {
every { Clock.hour() } returns 9
// Clock is restored when the test's scope closes
}every mocks the object in the current scope by itself; mockObject(Clock) records its calls for verify without
a stub. mockStatic(::function) does the same for top-level functions and properties, including extensions, and
mockStatic("com.example.FileKt") for a whole file. An overloaded or generic function cannot be passed by reference:
Kotlin does not resolve it. mockConstructor(Foo::class) with anyConstructed<Foo>() intercepts constructor calls in
code compiled with the plugin. Private functions of these classes are stubbed and verified by name:
every { spy["name"]() }.
Only the bytecode changes, so smart casts and initialization rules of main code stay as they are. Sealed classes stay
sealed with their members open, and inline and tailrec functions and @JvmField properties stay final. Every call of an intercepted
function or constructor reads a volatile field, and the plugin warns if the project applies maven-publish.
lokki { } setting |
Effect |
|---|---|
mockable(vararg patterns) |
Makes final classes, objects and top-level functions of main code mockable, see above. |
restricted(vararg patterns) |
Adds types whose mocks get the warning LOKKI_RESTRICTED_TYPE, as collections, maps, File, Path and types annotated with @DoNotMock do. A pattern is a package with its subpackages, a class or an annotation. |
testCompilations(vararg names) |
Applies the plugin to test compilations it does not recognize, such as a source set created without jvm-test-suite. |
applyToMain = true |
Lets main code create mocks, for modules with test utilities: applies the plugin and adds the runtime to main code. |
recordStackTraces = true |
Adds call sites of mock calls to failure reports. |
| Gradle property | Effect |
|---|---|
lokki.recordStackTraces=true |
Turns recordStackTraces on unless the build script sets it, for example with -P for one run. |
lokki.debug=true |
Reports the code of every file that lokki changes at the --info log level, for bug reports. |
lokki.mockablePublication.nowarn=true |
Disables the warning about mockable in published artifacts and non-debug Android variants. |
lokki.kotlinCompatibility.nowarn=true |
Disables the warning about a Kotlin version newer than the tested ones. |
Functions, matchers and their parameters keep MockK's names, so the MockK documentation applies.
Imports move from io.mockk to io.lokki, including annotations from io.mockk.impl.annotations, and names that
contain mockk or MockK change:
| MockK | lokki |
|---|---|
mockk, spyk
|
mock, spy
|
mockkClass |
mockClass |
mockkObject, mockkStatic, mockkConstructor, unmockkAll
|
mockObject, mockStatic, mockConstructor, unmockAll
|
@MockK, @RelaxedMockK, @SpyK, @InjectMockKs
|
@Mock, @RelaxedMock, @Spy, @InjectMocks
|
MockKExtension, MockKAnnotations, MockKException
|
LokkiExtension, MockAnnotations, LokkiException
|
MockKMatcherScope |
MatcherScope |
mockk-bdd |
lokki-bdd |
springmockk @MockkBean, @MockkSpyBean, MockkClear
|
@MockBean, @SpyBean, MockClear in lokki-spring, Spring 6.2.6+ |
Behaviour that differs:
every { } and verify { } are evaluated once, as ordinary code, and
any() never creates a placeholder value.every { car.door().open() } stubs car.door() with a child mock only when the call has no
answer yet; a stubbed car.door() keeps its answer. Relaxed mocks return child mocks only if the declaration that
creates them — a file, class, function, property or local variable — is annotated with @ChainedMocks: lokki
generates their classes at compile time, so the annotation marks where that cost is paid.null for nullable types and for a Java type parameter that allows it, such as
<T> T query(...), empty values for primitives, strings, collections, arrays and Optional, and the ZERO or
EPOCH of a type that declares one, such as Duration.ZERO. Any other return type throws LokkiException.spy(Foo(args)) constructs the spy itself, and spy(existing) forwards calls to existing,
so the calls that existing makes on itself bypass stubs and are not recorded.returns into the field. Code that
reads fields by reflection, such as Bean Validation of @field: constraints, needs a real object or
spy(Foo(args)).equals, hashCode and toString are not recorded.mock.javaClass is a generated subclass of the mocked class, not the class itself.coVerify does not run coroutines that are still waiting to be dispatched: MockK runs them as a side effect of its
runBlocking. Start them with CoroutineStart.UNDISPATCHED or let them complete before verifying.mockClass takes a class literal or a reified T::class, not a
KClass from a variable. A block passed to every or verify through your own function is an error.kotlin.time.Clock or a () -> UUID in place of
Instant.now() and UUID.randomUUID(), and give tests a fake or a mock. Where the code cannot take a parameter,
wrap the call in a top-level function of main code that matches a mockable pattern, for example
fun newId(): UUID = UUID.randomUUID(), and stub that function.every and verify need
the mock itself or a class proxy (proxyTargetClass = true). Class proxies of mocks work, including Spring's method
validation.lokkiScope { }. Kotest is not supported.LOKKI_* diagnostics only with the registry key
kotlin.k2.only.bundled.compiler.plugins.enabled set to false; the Gradle build always reports them../gradlew build # all tests and checks of the main build, including the ABI check
./gradlew -p integration-testing check # end-to-end tests and the MockK conformance suite
./gradlew :compiler-tests:test --tests '*Name*' # selected compiler plugin tests
./gradlew updateKotlinAbi # update the ABI dumps after a public API changeGradle downloads the JDK 25 toolchain of the build if it is missing. The Gradle plugin tests in build need an
Android SDK (ANDROID_HOME). The tests of lokki-maven download Maven and keep its local repository in
modules/lokki-maven/build.
lokki is a mocking library for Kotlin/JVM with the MockK DSL, implemented as a K2 compiler
plugin. mock<T>() compiles into an ordinary class that implements T, and the blocks of every { } and
verify { } compile into calls of the runtime instead of being executed. lokki keeps the DSL of MockK, not every
behaviour of its runtime: what MockK does with a Java agent either fails compilation with an alternative or
behaves differently.
mockObject, mockStatic and stubs added to shared mocks last until their scope — a JUnit 5
test or lokkiScope { } — closes.Status: alpha. Until 1.0, the API may change without a deprecation cycle, and mocks compiled by an older lokki may be rejected by a newer runtime: recompile test fixtures and test libraries when upgrading.
mockObject and mockStatic need 2.4.20 or newer.LokkiExtension; lokki raises an older JUnit 5 to 5.12.2.plugins {
kotlin("jvm") version "2.4.20"
id("io.github.wordyarc.lokki") version "0.3.0"
}The plugin applies to test compilations — test, testFixtures, JVM test suites, jvmTest of multiplatform
projects and Android test variants — and adds the runtime, io.github.wordyarc.lokki:lokki-core, to them. The
runtime includes the JUnit 5 extension. Register it for all tests in src/test/resources/junit-platform.properties:
junit.jupiter.extensions.autodetection.enabled=true
junit.jupiter.extensions.autodetection.include=io.lokki.junit5.*Every test then runs in its own scope.
class Job(val id: String)
interface JobQueue {
fun poll(): Job?
fun complete(job: Job)
}
class JobProcessor(private val queue: JobQueue) {
fun runNext(): Boolean {
val job = queue.poll() ?: return false
queue.complete(job)
return true
}
}import io.lokki.Runs
import io.lokki.every
import io.lokki.just
import io.lokki.mock
import io.lokki.verify
import kotlin.test.Test
import kotlin.test.assertTrue
class JobProcessorTest {
@Test
fun completesPolledJob() {
val queue = mock<JobQueue>()
val job = Job("42")
every { queue.poll() } returns job
every { queue.complete(any()) } just Runs
assertTrue(JobProcessor(queue).runNext())
verify(exactly = 1) { queue.complete(job) }
}
}mock<T>() accepts interfaces, open and abstract classes and function types, including generic types and suspend
members. Matchers, answers, coEvery/coVerify, ordered verification, relaxed mocks and spies work as in MockK,
with the differences listed in Coming from MockK.
Misuse fails compilation:
mock<Job>()
// e: 'com.example.Job' cannot be mocked: it is a final class. Extract an interface,
// make the class and its members open, or add it to lokki { mockable(...) }.
every { queue.complete(any()) }
// e: 'every' registers nothing without an answer: add 'returns', 'answers', 'throws'
// or 'just Runs' after the block.A scope is the lifetime of what a test does to mocks. When it closes, the stubs and recorded calls added in the test are removed, mocked objects, functions and constructors are restored, and the test fails if a mock call had no answer, even if the code under test caught the exception.
LokkiExtension runs every test class, test and dynamic test in its own scope and initializes @Mock,
@RelaxedMock, @Spy and @InjectMocks properties:
class JobProcessorTest {
@Mock
lateinit var queue: JobQueue
@Test
fun skipsEmptyQueue() {
every { queue.poll() } returns null
assertFalse(JobProcessor(queue).runNext())
verify { queue.poll() }
confirmVerified()
}
}The properties file from Installation registers the extension for all tests, and
@ExtendWith(LokkiExtension::class) for one class. @LokkiExtension.ConfirmVerification and
@LokkiExtension.CheckUnnecessaryStub on a test class run confirmVerified() and checkUnnecessaryStub() after each
test. @LokkiExtension.KeepMocks on a test class or method keeps the stubs and recorded calls of a test for the rest
of the class.
Without JUnit 5, lokkiScope { } provides the scope and MockAnnotations.init(this) initializes the properties.
Outside a scope, mocks, stubs and verification work, but nothing is removed or checked when a test ends. Calls that
change global state or act on all mocks of a test throw LokkiException there: mockObject, mockStatic,
mockConstructor, stubs of objects and top-level functions, and confirmVerified(), checkUnnecessaryStub() and
clearAllMocks() without arguments.
Without an agent, only calls of open members can be intercepted. Declarations of the module's main code that match a
mockable pattern are compiled differently: classes and their members are open in the bytecode, and objects and
top-level functions get interception points. A pattern is a package with its subpackages (com.example.**), a class,
an annotation or a function.
// build.gradle.kts
lokki {
mockable("com.example.**")
}// src/main/kotlin
object Clock {
fun hour(): Int = 12
}// src/test/kotlin, in a scope
@Test
fun morning() {
every { Clock.hour() } returns 9
// Clock is restored when the test's scope closes
}every mocks the object in the current scope by itself; mockObject(Clock) records its calls for verify without
a stub. mockStatic(::function) does the same for top-level functions and properties, including extensions, and
mockStatic("com.example.FileKt") for a whole file. An overloaded or generic function cannot be passed by reference:
Kotlin does not resolve it. mockConstructor(Foo::class) with anyConstructed<Foo>() intercepts constructor calls in
code compiled with the plugin. Private functions of these classes are stubbed and verified by name:
every { spy["name"]() }.
Only the bytecode changes, so smart casts and initialization rules of main code stay as they are. Sealed classes stay
sealed with their members open, and inline and tailrec functions and @JvmField properties stay final. Every call of an intercepted
function or constructor reads a volatile field, and the plugin warns if the project applies maven-publish.
lokki { } setting |
Effect |
|---|---|
mockable(vararg patterns) |
Makes final classes, objects and top-level functions of main code mockable, see above. |
restricted(vararg patterns) |
Adds types whose mocks get the warning LOKKI_RESTRICTED_TYPE, as collections, maps, File, Path and types annotated with @DoNotMock do. A pattern is a package with its subpackages, a class or an annotation. |
testCompilations(vararg names) |
Applies the plugin to test compilations it does not recognize, such as a source set created without jvm-test-suite. |
applyToMain = true |
Lets main code create mocks, for modules with test utilities: applies the plugin and adds the runtime to main code. |
recordStackTraces = true |
Adds call sites of mock calls to failure reports. |
| Gradle property | Effect |
|---|---|
lokki.recordStackTraces=true |
Turns recordStackTraces on unless the build script sets it, for example with -P for one run. |
lokki.debug=true |
Reports the code of every file that lokki changes at the --info log level, for bug reports. |
lokki.mockablePublication.nowarn=true |
Disables the warning about mockable in published artifacts and non-debug Android variants. |
lokki.kotlinCompatibility.nowarn=true |
Disables the warning about a Kotlin version newer than the tested ones. |
Functions, matchers and their parameters keep MockK's names, so the MockK documentation applies.
Imports move from io.mockk to io.lokki, including annotations from io.mockk.impl.annotations, and names that
contain mockk or MockK change:
| MockK | lokki |
|---|---|
mockk, spyk
|
mock, spy
|
mockkClass |
mockClass |
mockkObject, mockkStatic, mockkConstructor, unmockkAll
|
mockObject, mockStatic, mockConstructor, unmockAll
|
@MockK, @RelaxedMockK, @SpyK, @InjectMockKs
|
@Mock, @RelaxedMock, @Spy, @InjectMocks
|
MockKExtension, MockKAnnotations, MockKException
|
LokkiExtension, MockAnnotations, LokkiException
|
MockKMatcherScope |
MatcherScope |
mockk-bdd |
lokki-bdd |
springmockk @MockkBean, @MockkSpyBean, MockkClear
|
@MockBean, @SpyBean, MockClear in lokki-spring, Spring 6.2.6+ |
Behaviour that differs:
every { } and verify { } are evaluated once, as ordinary code, and
any() never creates a placeholder value.every { car.door().open() } stubs car.door() with a child mock only when the call has no
answer yet; a stubbed car.door() keeps its answer. Relaxed mocks return child mocks only if the declaration that
creates them — a file, class, function, property or local variable — is annotated with @ChainedMocks: lokki
generates their classes at compile time, so the annotation marks where that cost is paid.null for nullable types and for a Java type parameter that allows it, such as
<T> T query(...), empty values for primitives, strings, collections, arrays and Optional, and the ZERO or
EPOCH of a type that declares one, such as Duration.ZERO. Any other return type throws LokkiException.spy(Foo(args)) constructs the spy itself, and spy(existing) forwards calls to existing,
so the calls that existing makes on itself bypass stubs and are not recorded.returns into the field. Code that
reads fields by reflection, such as Bean Validation of @field: constraints, needs a real object or
spy(Foo(args)).equals, hashCode and toString are not recorded.mock.javaClass is a generated subclass of the mocked class, not the class itself.coVerify does not run coroutines that are still waiting to be dispatched: MockK runs them as a side effect of its
runBlocking. Start them with CoroutineStart.UNDISPATCHED or let them complete before verifying.mockClass takes a class literal or a reified T::class, not a
KClass from a variable. A block passed to every or verify through your own function is an error.kotlin.time.Clock or a () -> UUID in place of
Instant.now() and UUID.randomUUID(), and give tests a fake or a mock. Where the code cannot take a parameter,
wrap the call in a top-level function of main code that matches a mockable pattern, for example
fun newId(): UUID = UUID.randomUUID(), and stub that function.every and verify need
the mock itself or a class proxy (proxyTargetClass = true). Class proxies of mocks work, including Spring's method
validation.lokkiScope { }. Kotest is not supported.LOKKI_* diagnostics only with the registry key
kotlin.k2.only.bundled.compiler.plugins.enabled set to false; the Gradle build always reports them../gradlew build # all tests and checks of the main build, including the ABI check
./gradlew -p integration-testing check # end-to-end tests and the MockK conformance suite
./gradlew :compiler-tests:test --tests '*Name*' # selected compiler plugin tests
./gradlew updateKotlinAbi # update the ABI dumps after a public API changeGradle downloads the JDK 25 toolchain of the build if it is missing. The Gradle plugin tests in build need an
Android SDK (ANDROID_HOME). The tests of lokki-maven download Maven and keep its local repository in
modules/lokki-maven/build.