用于 Kotlin 多平台移动设备的 Hamcrest 和 Mockk

Hamcrest and Mockk for Kotlin Multiplatform Mobile

我正在尝试在 KMM 项目的 shared 模块中编写测试。在共享模块的 build.gradle.kts 文件中,我有以下内容:

sourceSets {
    val commonMain by getting
    val commonTest by getting {
        dependencies {
            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))


            //TODO: Hamcrest
            //implementation("junit:junit:4.12")
            //implementation("org.hamcrest:hamcrest-library:1.3")

            //TODO: Mockk
            //implementation("io.mockk:mockk:1.10.4")
        }
    }
    //...
}

我也试过:

implementation(kotlin("[library]"))

结果相同:测试不再被 IDE 识别,我无法 运行 它们。

遗憾的是,没有 K/N 支持 (AFAIK) 的模拟库。

这里是Mockk's K/N and Mockk's K/JS issue for future reference or you could also check out Touchlab's Karmok

对于 Hamcrest,请参阅他们的问题 here

我收到了 KMM 团队的回复 - 我想我会把它放在这里以供参考

You can use only multiplatform dependencies that support all declared targets in common source-set, because this could will be used for compilation for all the targets. Junit is not a multiplatform library, it’s JVM, so you should add it to your jvm target source-set (androidMain if you declared android() target). Check this project: https://github.com/Kotlin/kmm-sample/blob/master/shared/build.gradle.kts for example.

The same issue relates to other dependencies - they are not multiplatform, so you can’t use them in a commons source-set.