Getting started

This page takes a JVM project from nothing to a working mock and a working fake. For Kotlin Multiplatform projects, see Setup.

  1. Apply the Gradle plugins and activate the test helper:

    build.gradle.kts
    plugins {
        kotlin("jvm") version "2.4.20"
        id("com.google.devtools.ksp") version "2.3.12" (1)
        id("org.kodein.mock.mockmp") version "3.5.5" (2)
    }
    
    kotlin {
        jvmToolchain(11)
        // Your Kotlin configuration
    }
    
    mockmp { (3)
        onTest {
            withHelper() // or withHelper(junit5)
        }
    }
    1 Apply the KSP plugin that corresponds to the Kotlin version you are using.
    2 Apply the MocKMP plugin.
    3 Apply MocKMP to your test source sets, after declaring the kotlin targets.
  2. Create a test class that declares injected mocks and fakes:

    MyTest.kt
    class MyTest : TestsWithMocks() { (1)
        override fun setUpMocks() = mocker.injectMocks(this) (2)
    
        @Mock lateinit var view: View
        @Fake lateinit var model: Model
    
        val controller by withMocks { Controller(view = view, firstModel = model) }
    
        @Test fun controllerTest() {
            every { view.render(isAny()) } returns true
            controller.start()
            verify { view.render(model) }
        }
    }
    1 The TestsWithMocks super class eases the use of MocKMP in your tests, but is not mandatory.
    2 This is mandatory and cannot be generated. You need to build your project at least once for your IDE to see the injectMocks function.

    Every property annotated by @Mock, annotated by @Fake or delegated to withMocks is reset fresh between each test.

That’s it! From here, Mocking types and Faking types explain what you can do with a mock and a fake, and Setup covers the other platforms.