Recently, when working with Deezer on a Kotlin/Multiplatform project, we came across multiple unit tests that were written in src/androidTest/kotlin, meaning that they were only run on Android. When investigating why that choice was made, the answer became evident: there is no mocking system on Kotlin/Multiplatform, and MockK only supports JVM & Android.
Without a mocking library, the only option left is to write every mock by hand, and to fill our fake data classes one field at a time. That is tedious, repetitive and, let’s be honest, a little soul-crushing. We wanted to write tests, not boilerplate.

Mocking with reflection
JVM reflection is an amazingly powerful API, allowing libraries to instrument existing classes and create new implementations at runtime. When you write:
val repo = mockk<Repository>()
every { repo.getUser(id = any()) } returns fakeUser()
assertEquals(fakeUser(), repo.getUser(42))…the mocking library:
- Creates a new implementation of the
Repositorytype - Sets the behavior of the
getUsermethod on this mock to return a fakeUser, whatever theidparameter.
Furthermore, a JVM mocking framework is able to create “fake objects” on demand:
val repo = mockk<Repository>(relaxed = true)
assertEquals("", repo.getUser(42).name)All of that is done at runtime, by instrumenting the JVM runtime, which is not possible with two of the three targets of Kotlin/Multiplatform (Kotlin/JS & Kotlin/Native).
A Kotlin Symbol Processor
Google KSP allows you to inspect Kotlin code at compile time. In essence, it provides a reflection API available at compile time (rather than at runtime).
KSP also allows you to generate Kotlin code according to the code being inspected. This is meta-programming: writing code to generate code according to code.
With a Kotlin Symbol Processor, we can generate implementations of interfaces at compile time, and delegate their behavior to a mocker.
Introducing MocKMP
The MocKMP project consists of multiple moving parts that allow you to easily generate and configure mocks:
- A runtime that provides the mocker API
- A Kotlin Symbol Processor that generates configurable mocks
- A test helper library to make the use of mocks as easy as possible
- A Gradle plugin to make all these moving parts work together
When we started working on MocKMP, we defined an important requirement that we felt was needed in order to make this library as easy as possible to use: keep a “standard” mocking API.
Mocking is a pretty standard task when writing unit tests, and we felt that keeping an established API semantic would make the port of unit tests to Kotlin/Multiplatform as effortless as possible.
No need to reinvent an API when one has already existed for years, and has been widely accepted as a standard.
The following declaration works as expected in MocKMP:
// Configures repo.getUser mock method
every { repo.getUser(isAny()) } returns fakeUser()
// Calls the repo.getUser mock method
assertEquals(fakeUser(), repo.getUser(42))
// Verifies that the repo.getUser mock method has been called
verify { repo.getUser(42) }Mocks and fakes
Because MocKMP works without runtime instrumentation, we had to introduce a few more definitions and restrictions than a “regular” mocking library brings.
A mock is an implementation of an interface whose behavior can be later configured and calls verified.
You can only mock interfaces, abstract or open classes. This is because there is no way to create a compile-time implementation of a non-open class in Kotlin. We encourage you to use interfaces as much as possible.
A fake is a concrete class instance filled with inconsequential data:
- Nullables are null
- Booleans are false
- Numerics are 0
- Strings are ""
- Enums are their first declared value
You can only fake concrete data structures (meaning concrete classes containing only concrete classes). As a rule of thumb, only data classes should be faked. This is because creating a fake needs no context while creating a mock needs a mocker.
Injecting mocks and fakes
This article is not going to go in depth into the usage of MocKMP. If you are interested, you can read its documentation, which is rather quick and simple to understand. Instead, let’s analyze a use case:
class MyTests : TestsWithMocks() { // 1
override fun setUpMocks() = injectMocks(mocker) // 2
@Mock lateinit var view: View // 3
@Fake lateinit var model: Model // 4
val controller by withMocks { Controller(view = view, firstModel = model) } // 5
@Test fun controllerTest() {
every { view.render(isAny()) } returns true // 6
controller.start() // 7
verify { view.render(model) } // 8
}
}- Optional
TestsWithMocksinheritance.
Inherit fromTestsWithMocksand your tests get a little syntactic sugar, which makes them easier to write and read. It’s entirely optional: the documentation shows how to get every feature without it. - Implementation of
setUpMocks.
When your class has properties annotated with@Mockor@Fake, the processor generates aninjectMocksextension function for it (here,MyTests.injectMocks). Since that function is generated and differs for each class,TestsWithMockscan’t call it by itself: you tell it how by overridingsetUpMocks. - Injecting a mock.
TheTestsWithMocksclass registers a@BeforeTesthook that will inject a freshViewmock for each test. The property islateinitbecause it will be injected before each test (and not once at initialization). - Injecting a fake.
Same mechanism for theModelfake: a fresh one is injected before each test, hencelateinit. - Injecting mock and fake dependent types.
Properties built from mocks and fakes use thewithMocksdelegate. In this example, a newControllerwill be created with a freshviewandmodelbefore each new test is run. - Configuring mock behavior.
To define the behavior of a mock instance method in a test, use theeverysyntax. Each of the method arguments must define an argument constraint. Here,isAny()means “whatever the value”: no constraint on the argument. - Using mocks and fakes.
Here, we assume that by callingcontroller.start, the controller will use theviewmock that was passed to it as a constructor parameter. - Verifying mock calls.
Since assuming is not enough, we need to actually verify that the calls were made. Here, we verify thatview.renderwas called with themodelparameter; the test fails if it wasn’t called, or was called with something else.
A processor for Multiplatform
So, if we can’t use reflection, how does it work?
Basically, all we need is a way to tell the system “this method was called, with this argument constraint.”
If we consider the View interface:
interface View {
fun render(model: Model)
}Then, the MockView class that will be generated for it looks like:
internal class MockView(private val mocker: Mocker) : View {
override fun render(model: Model) =
mocker.register(this, "render(com.mybusiness.Model)", model)
}With this generated behavior, we inform the Mocker (which is the object that handles mocking and verification) that the render method was called, and we leave the actual behavior and return value to be handled by the mocker itself.
In essence, the processor is generating a proxy.
It is also generating methods to create fakes:
internal fun fakeModel() =
Model(
user = fakeUser(), // Fakes can contain fakes
lasUpdate = 0, // numerics are 0
state = null // nullables are null
)Finally, it generates an injectMocks extension function for each class that contains injection annotations:
internal fun MyTests.injectMocks(mocker: Mocker) {
view = MockView(mocker)
model = fakeModel()
}Thoughtful API changes
We have allowed ourselves to slightly change the API of MocKMP to differ from “traditional” mocking frameworks such as MockK or Mockito-Kotlin. We would like to draw your attention to these specificities of the MocKMP API:
- All argument constraints start with
is.
Instead of:every { view.render(any()) } returns true,
write:every { view.render(isAny()) } returns true.
This allows for discoverability. If you want to find all available constraints in your IDE, typeisfollowed by the completion shortcut (ctrl+spacein IDEA) and you’ll find them all (isAny,isEqual,isNotEqual,isSame,isNotSame,isNull,isNotNull). - There is no
every { foo.bar() } just Runs. This is Kotlin: no function “just runs”. A function always has a return value. If it returnsUnit, then write it, just like any other:every { foo.bar() } returns Unit. - The
verifyfunction is exhaustive by default.
This means thatverify {}actually verifies that no calls were made to any mock instance.
You can disable this withverify(exhaustive = false). - The
verifyfunction is ordered by default.
This means thatverify { m.foo() ; m.bar() }verifies thatfooandbarwere called onmin that order.
You can disable this withverify(inOrder = false).
Conclusion
No reflection, no problem: since two of the three Kotlin/Multiplatform targets can’t instrument the JVM, we generate the mocks at compile time instead, and we kept the API familiar so that porting Deezer’s Android-only tests stays as painless as possible. If you give MocKMP a try, we’d love to hear what you think.
These articles might interest you
Tutorials, lessons learned and best practices around KOSI and the Kotlin ecosystem, from the people who maintain the libraries.
17/11/2026
Inara has no magic: read the code it writes
In most DI frameworks, a module is an anonymous bag of bindings, and dependencies are looked up by type. Inara gives modules an identity. Let's open the hood and read the code it generates: there is no trick, only plain Kotlin you can Cmd+click through.
Read more
09/11/2026
LARPing enthusiasm: the ghost of disengagement
I can hand the work to AI, but not my judgment or my product sense. A story of sliding into disengagement, and of a comeback still in progress.
Read more
02/11/2026
Inara: Inject your dependencies at compile time
I created Kodein, yet I now believe it is detrimental to the Kotlin Multiplatform ecosystem. Meet Inara, a Kotlin Symbol Processor that resolves your dependencies at compile time, and learn why we are sunsetting Kodein.
Read more
26/10/2026
Agentic coding has killed cross-platform frameworks!
Agents made it cheap to write native code twice, so is cross-platform dead? Not quite: what changed is the question. Share what must be identical, and check it once.
Read more
19/10/2026
LARPing enthusiasm: the AI creativity frenzy
AI lets me build in weeks what once took years, and it feels like a drug. Am I really creating, or just LARPing enthusiasm? A personal reflection on agency, will and shared code.
Read more
04/09/2025
Use Swift in a Kotlin/Multiplatform library
Need to use a Swift-only iOS API such as CryptoKit in your Kotlin/Multiplatform library? Salomon shows how to bridge it through Objective-C and Kotlin/Native cinterop, with tested code.
Read more