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.
- No instrumentation. No Java agent, proxies or runtime class generation.
- Errors at compile time. Unmockable types, calls that cannot be intercepted and stubs without an answer fail the build.
- Scoped state.
mockObject,mockStaticand stubs added to shared mocks last until their scope — a JUnit 5 test orlokkiScope { }— 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.
- Kotlin 2.4.0 – 2.5.0-RC;
mockObjectandmockStaticneed 2.4.20 or newer. - Gradle 9.0 or newer.
- JVM target 17 or newer for test code.
- JUnit Jupiter 5.12 or newer, including 6.x, for
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:
- The receiver and arguments of the call in
every { }andverify { }are evaluated once, as ordinary code, andany()never creates a placeholder value. - A call chain such as
every { car.door().open() }stubscar.door()with a child mock only when the call has no answer yet; a stubbedcar.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. - A relaxed mock returns
nullfor nullable types and for a Java type parameter that allows it, such as<T> T query(...), empty values for primitives, strings, collections, arrays andOptional, and theZEROorEPOCHof a type that declares one, such asDuration.ZERO. Any other return type throwsLokkiException. - Spies are not copies:
spy(Foo(args))constructs the spy itself, andspy(existing)forwards calls toexisting, so the calls thatexistingmakes on itself bypass stubs and are not recorded. - A stub of a property does not write its backing field: MockK copies the value of
returnsinto the field. Code that reads fields by reflection, such as Bean Validation of@field:constraints, needs a real object orspy(Foo(args)). - Calls of
equals,hashCodeandtoStringare not recorded. mock.javaClassis a generated subclass of the mocked class, not the class itself.coVerifydoes not run coroutines that are still waiting to be dispatched: MockK runs them as a side effect of itsrunBlocking. Start them withCoroutineStart.UNDISPATCHEDor let them complete before verifying.
- JVM only: Kotlin/JVM, Android and the JVM target of multiplatform projects. Android instrumented tests compile with the plugin but have not been run on ART.
- Mocks are created and stubbed only in Kotlin code compiled with the plugin; Java code can only call them.
- The mocked type must be known at compile time:
mockClasstakes a class literal or a reifiedT::class, not aKClassfrom a variable. A block passed toeveryorverifythrough your own function is an error. - Final classes, objects, functions and constructors from libraries and Java static methods cannot be mocked. Pass
them to the code under test instead, for example a
kotlin.time.Clockor a() -> UUIDin place ofInstant.now()andUUID.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 amockablepattern, for examplefun newId(): UUID = UUID.randomUUID(), and stub that function. - A JDK dynamic proxy of a mock, such as an interface-based Spring AOP proxy, is not a mock:
everyandverifyneed the mock itself or a class proxy (proxyTargetClass = true). Class proxies of mocks work, including Spring's method validation. - No JUnit 4 integration: use
lokkiScope { }. Kotest is not supported. - IntelliJ IDEA shows
LOKKI_*diagnostics only with the registry keykotlin.k2.only.bundled.compiler.plugins.enabledset tofalse; 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.