Setup

MocKMP is applied with its Gradle plugin, which adds the runtime, the processor and, if you ask for it, the test helper to the source sets you choose. You never declare these artifacts by hand:

Artifact Purpose

org.kodein.mock.mockmp

The Gradle plugin, which wires everything below.

org.kodein.mock:mockmp-runtime

The Mocker, the argument constraints and the fake support, on every platform.

org.kodein.mock:mockmp-processor

The KSP processor that generates the mocks and fakes.

org.kodein.mock:mockmp-test-helper

The TestsWithMocks helper for JUnit 4, added by withHelper().

org.kodein.mock:mockmp-test-helper-junit5

The TestsWithMocks helper for JUnit 5, added by withHelper(junit5).

MocKMP uses KSP but does not install it in your project. This is because the KSP version to use depends on the version of Kotlin you are using. It is therefore your responsibility to install it on your project along with MocKMP.

Kotlin Multiplatform

First, add the Gradle plugins:

build.gradle.kts
plugins {
    kotlin("multiplatform") version "2.4.20"
    id("com.google.devtools.ksp") version "2.3.12"
    id("org.kodein.mock.mockmp") version "3.5.5"
}

Then, configure your Kotlin targets and dependencies. For example:

build.gradle.kts
kotlin {
    androidTarget()
    jvmToolchain(11)

    iosArm64()
    iosSimulatorArm64()

    sourceSets {
        commonTest.dependencies {
            implementation(kotlin("test"))
        }
        androidUnitTest.dependencies {
            implementation(kotlin("test-junit"))
        }
    }
}

Finally, apply MocKMP to your test source sets:

This must be done after configuring Kotlin targets.
build.gradle.kts
mockmp {
    onTest()
    // OR
    onTest {
        withHelper()
    }
}

The withHelper() is optional and will add the TestsWithMocks helper class dependency to your project. It will by default try to detect which JUnit version your JVM / Android target is using and may fail.
You can force the JUnit version by using withHelper(junit4) or withHelper(junit5):

build.gradle.kts
mockmp {
    onTest {
        withHelper(junit5)
    }
}

JVM

On a JVM-only project, apply the same plugins to a kotlin("jvm") module:

build.gradle.kts
plugins {
    kotlin("jvm") version "2.4.20"
    id("com.google.devtools.ksp") version "2.3.12"
    id("org.kodein.mock.mockmp") version "3.5.5"
}

kotlin {
    jvmToolchain(11)
}

mockmp {
    onTest {
        withHelper()
    }
}

Applying to main source sets

In some cases, you may need to apply the processor to the common-main source-set instead of common-test.

build.gradle.kts
mockmp {
    onMain {
        public()
    }
}
The withHelper() JUnit detection will fail when applying MocKMP to main source-sets. If you want to add the TestsWithMocks helper class to your main source-sets, you must specify explicitly which JUnit version to use.

Other configurations

build.gradle.kts
mockmp {
    onTest { // or onMain

        // By default, MocKMP will be applied to all targets.
        // This is therefore not necessary (as it is the default).
        allTargets()
        // But you can instead apply it to only a subset of specific targets.
        specificTargets("jvm", "iosSimulatorArm64")

        // If you want the generated mocks & fakes to be public instead of internal.
        public()
        // or
        public(true)

        // By default, the mocks & fakes accessors will be generated in the
        // org.kodein.mock.generated package.
        // You may change this if you have multiple modules using MocKMP, to avoid
        // collision between generated accessors.
        accessorsPackage("com.myproject.mockmp.generated")

    }
}

What gets generated

For every type you request, the processor generates plain Kotlin sources under build/generated/ksp, which are compiled with your tests:

Request Generated

@Mock property, or @UsesMocks type Service

A MockService class implementing Service, in the file MockService.kt.

@Fake property, or @UsesFakes type Greeting

A fakeGreeting() function, in the file fakeGreeting.kt.

A class OptionsTests with @Mock or @Fake properties

A Mocker.injectMocks(receiver: OptionsTests) extension, in the file OptionsTests_injectMocks.kt.

For example, for a test class declaring @Mock lateinit var service: Service and @Fake lateinit var greeting: Greeting:

OptionsTests_injectMocks.kt
public fun Mocker.injectMocks(`receiver`: OptionsTests) {
    receiver.service = MockService(this)
    receiver.greeting = fakeGreeting()
}

You never call these generated classes yourself: use mocker.mock<T>(), fake<T>() and injectMocks, which are the supported entry points. Their accessors are generated in the org.kodein.mock.generated package, which you can change with accessorsPackage, as shown above.