Getting started
-
Apply the Gradle plugin and activate the helper dependency:
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" (2) } kotlin { jvmToolchain(17) // Your Koltin/Multiplatform 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 (must be configured after declaring kotlin
targets). -
Create a test class that declares injected mocks and fakes:
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 towithMocks
will be reset fresh between each test.