Migrate MocKMP from 1.17 to 2.0

The new MocKMP 2 changes the way:

  • It is configured in your project.

  • How mocks, fakes, and injectors are accessed.

The rest is the same: how mocks are configured and verified, as well as how fakes and injectors work, remains unchanged.

Gradle build script

Using the MocKMP Gradle plugin is now the only way to use MocKMP in your project. It is no longer possible to install it manually on your project without using the Gradle plugin.

Add the KSP plugin

The MocKMP plugin does not apply the KSP plugin anymore. You therefore need to add the KSP plugin according to the Kotlin version you are using:

build.gradle.kts
plugins {
    kotlin("multiplatform") version "2.0.21"
    id("com.google.devtools.ksp") version "2.0.21-1.0.28" (1)
    id("org.kodein.mock.mockmp") version "2.0.0"
}
1 Adding the KSP plugin in addition to the MocKMP plugin is now mandatory.

Migrate the mockmp block to the new syntax

After defining the Kotlin targets, migrate to the new MocKMP configuration.

The minimum required to activate MocKMP on your test source sets is:

build.gradle.kts
mockmp {
    onTest()
}
installWorkaround() has been removed, as it was replaced by the onTest & onMain functions.

If you are using the TestWithMocks helper class (or ITestWithMocks helper interface), add it to dependencies:

build.gradle.kts
mockmp {
    onTest {
        withHelper() (1)
    }
}
1 Note that you can specify which JUnit version you are using (instead of relying on detection).

To see additional configuration options, as well as how to apply MocKMP to the main source sets, have a look at the setup documentation .

Kotlin sources

MocKMP 1 ran the code generation on JVM only and used generated code on all targets. MocKMP 2 now runs the code generator individually on each target. Because of this, it is not possible anymore to access mock classes & fake creator functions by generated name.

MocKMP 2 introduces accessor functions that are accessible from common sources.

Migrate mock classes to the new accessor

Update your mock creation from MockX(mocker) to mocker.mock<X>():

MyTest.kt
interface Validator {
    // ...
}

@UsesMocks(Validator::class)
class ValidatorTests : TestsWithMocks() {
    @Test
    fun testSimpleValidation() {
        // val validator = MockValidator(mocker) (1)
        val validator = mocker.mock<Validator>() (2)

        // ...
    }
}
1 Old syntax, not supported anymore as MockFoo is not in common sources anymore (but generated for each target).
2 New syntax.
The Mocker.mock<T> extension function is generated by MocKMP and will not be available before first build.

Migrate fake functions to the new accessor

Update your fake creation from fakeX() to fake<X>():

MyTest.kt
data class User(
    val id: Long,
    val name: String,
    //...
)

@UsesFakes(User::class)
class DatabaseTests {
    @Test
    fun testSave() {
        // val user = fakeUser() (1)
        val user = fake<User>() (2)

        database.save(user)

        // ...
    }
}
1 Old syntax, not supported anymore as fakeUser is not in common sources anymore (but generated for each target).
2 New syntax.
The fake<T> function is generated by MocKMP and will not be available before first build.

Migrate injection to the new accessor

Update mocks & fakes injections from receiver.injectMocks(mocker) to mocker.injectMocks(receiver):

MyTest.kt
class DatabaseTests : TestsWithMocks() {
    @Mock lateinit var validator: Validator
    @Fake lateinit var user: User

    // override fun setUpMocks() = injectMocks(mocker) (1)
    override fun setUpMocks() = mocker.injectMocks(this) (2)
}
1 Old syntax, not supported anymore as T.injectMocks(Mocker) is not in common sources anymore (but generated for each target).
2 New syntax.
The Mocker.injectMocks<T> extension function is generated by MocKMP and will not be available before first build.