MocKMP Setup
Standard test setup
First, add the gradle plugins:
build.gradle.kts
plugins {
kotlin("multiplatform") version "2.0.21"
id("com.google.devtools.ksp") version "2.0.21-1.0.28"
id("org.kodein.mock.mockmp") version "2.0.0"
}
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. |
Then, configure your Kotlin targets and dependencies. For example:
build.gradle.kts
kotlin {
androidTarget()
jvmToolchain(17)
iosArm64()
iosSimulatorArm64()
iosX64()
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 build.gradle.kts
|
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 explicitely 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")
}
}