Skip to content

MocKMP: a mocking processor for Kotlin/Multiplatform

  • Kotlin
  • Multiplatform
  • Deep dive
  • 8 min.

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:

mocking-with-mock.kt
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 Repository type
  • Sets the behavior of the getUser method on this mock to return a fake User, whatever the id parameter.

Furthermore, a JVM mocking framework is able to create “fake objects” on demand:

relaxed-mockk.kt
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:

mocking-with-MocKMP.kt
// 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:

tests-with-MocKMP.kt
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
  }
}
  1. Optional TestsWithMocks inheritance.
    Inherit from TestsWithMocks and 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.
  2. Implementation of setUpMocks.
    When your class has properties annotated with @Mock or @Fake, the processor generates an injectMocks extension function for it (here, MyTests.injectMocks). Since that function is generated and differs for each class, TestsWithMocks can’t call it by itself: you tell it how by overriding setUpMocks.
  3. Injecting a mock.
    The TestsWithMocks class registers a @BeforeTest hook that will inject a fresh View mock for each test. The property is lateinit because it will be injected before each test (and not once at initialization).
  4. Injecting a fake.
    Same mechanism for the Model fake: a fresh one is injected before each test, hence lateinit.
  5. Injecting mock and fake dependent types.
    Properties built from mocks and fakes use the withMocks delegate. In this example, a new Controller will be created with a fresh view and model before each new test is run.
  6. Configuring mock behavior.
    To define the behavior of a mock instance method in a test, use the every syntax. Each of the method arguments must define an argument constraint. Here, isAny() means “whatever the value”: no constraint on the argument.
  7. Using mocks and fakes.
    Here, we assume that by calling controller.start, the controller will use the view mock that was passed to it as a constructor parameter.
  8. Verifying mock calls.
    Since assuming is not enough, we need to actually verify that the calls were made. Here, we verify that view.render was called with the model parameter; 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:

View.kt
interface View {
  fun render(model: Model)
}

Then, the MockView class that will be generated for it looks like:

(Generated)MockView.kt
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:

(Generated)fakeModel.kt
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:

(Generated)MyTests_injectMocks.kt
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:

  1. 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, type is followed by the completion shortcut (ctrl+space in IDEA) and you’ll find them all (isAny, isEqual, isNotEqual, isSame, isNotSame, isNull, isNotNull).
  2. There is no every { foo.bar() } just Runs. This is Kotlin: no function “just runs”. A function always has a return value. If it returns Unit, then write it, just like any other: every { foo.bar() } returns Unit.
  3. The verify function is exhaustive by default.
    This means that verify {} actually verifies that no calls were made to any mock instance.
    You can disable this with verify(exhaustive = false).
  4. The verify function is ordered by default.
    This means that verify { m.foo() ; m.bar() } verifies that foo and bar were called on m in that order.
    You can disable this with verify(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.

Kotlin Multiplatform Architecture Deep dive
7 min.

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
Opinion
8 min.

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
Kotlin Multiplatform Architecture Deep dive
6 min.

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
IA Kotlin Multiplatform Opinion
7 min.

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
IA Opinion
6 min.

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
Kotlin Multiplatform Deep dive
10 min.

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