# MocKMP : a Mocking processor for Kotlin/Multiplatform

DevFeed: [MocKMP : a Mocking processor for Kotlin/Multiplatform](<https://devfeed.tech/articles/mockmp-a-mocking-processor-for-kotlin-multiplatform-22863.md>)

Original publisher: [Read original article](<https://medium.com/kodein-koders/mockmp-a-mocking-processor-for-kotlin-multiplatform-51957c484fe5?source=rss----f311f45ef54---4>)

Author: Salomon BRYS

Published: 2022-01-17T13:29:28Z

Content type: tutorial

Language: en

Sources: [Kodein Koders - Medium](<https://devfeed.tech/sources/kodein-koders-medium.md>)

Topics: [Kotlin Multiplatform](<https://devfeed.tech/topics/kotlin-multiplatform.md>), [Mocking](<https://devfeed.tech/topics/mocking.md>), [Code](<https://devfeed.tech/topics/code.md>), [test](<https://devfeed.tech/topics/test.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [gradle](<https://devfeed.tech/tags/gradle.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [kotlin-native](<https://devfeed.tech/tags/kotlin-native.md>), [ksp](<https://devfeed.tech/tags/ksp.md>), [mocking](<https://devfeed.tech/tags/mocking.md>), [multiplatform](<https://devfeed.tech/tags/multiplatform.md>), [test](<https://devfeed.tech/tags/test.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This article introduces MocKMP, a mocking solution for Kotlin Multiplatform. It explains why JVM reflection-based mocking does not work for Kotlin/JS and Kotlin/Native, and describes using Kotlin Symbol Processing to generate mock implementations at compile time. MocKMP includes a runtime API, a symbol processor, test helpers, and a Gradle plugin.

## Source excerpt

MocKMP : a Mocking processor for Kotlin/MultiplatformPhoto by Stefano Pollio on Unsplash 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 investing why that choice was made, the answer became evident : there is no mocking system on Kotlin/Multiplatform, and Mockk only supports JVM & Android. Mocking with reflection The JVM reflection is an amazing powerful API, allowing libraries to instrument existing classes and create new implementations at run time. When you write : https://medium.com/media/28a2defcfc5197a7e44d270d4414acd1/href ...the mocking library : Creates a new implementation of the Repository type Sets the behaviour 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: https://medium.com/media/2e91c376131e77315f319705fd45e8b2/href All of that is done at run time, by instrumenting the JVM runtime, which is not possible with 2 of the 3 types of targets of Kotlin/Multiplatform (Kotlin/JS & Kotlin/Native). A Kotlin Symbol Processor Google KSP allows to inspect Kotlin code at compile time. In essence, it provides a reflection API available at compile time (rather than at run time). KSP also allows 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 implementation of interfaces at compile time, and delegate the implementation of said implementations to a mocker delegate. Introducing MocKMP The MocKMP project consists of multiple moving parts that allow 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 ma