未解决的参考:mock

Sun*_*y13 2 junit kotlin-multiplatform mockk

我已在->模块中导入mockk库。测试类中没有导入错误,但是当我运行测试时,我收到如下错误:commonTestshared

Unresolved reference: every
Unresolved reference: mockk
Unresolved reference: verify
Run Code Online (Sandbox Code Playgroud)

在我使用mock库方法的所有地方。错误的原因可能是什么?

我的测试示例在控制台中出现错误:

class DefaultAppPreferenceStorageTest {

    val appPreference = mockk<AppPreference>() //Unresolved reference: mockk
    val jsonService = mockk<JsonService>() //Unresolved reference: mockk

    val jsonKey = "key"
    val value = 1
    val stringValue = "$value"
    val defaultIntValue = Random.nextInt()

    val storage = DefaultAppPreferenceStorage(
        appPreference,
        jsonService
    )

    inner class PutJsonTest {

        @BeforeTest
        fun beforeEachTest() {
            every { jsonService.mapToString(value) } returns stringValue //Unresolved reference: every

            storage.putJson(jsonKey, value)
        }

        @Test
        fun testPutJson() {
            verify(verifyBlock = { jsonService.mapToString(value) }) //Unresolved reference: verify
            verify(verifyBlock = { appPreference.putString(jsonKey, stringValue) }) //Unresolved reference: verify
        }
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

更新依赖 关系

const val mockk = "1.12.5"

const val mockk = "io.mockk:mockk-common:${Version.mockk}"
Run Code Online (Sandbox Code Playgroud)
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation(ShareTestDependencies.mockk)
                implementation(ShareTestDependencies.coroutinesTest)
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ste*_*ers 5

从模拟 1.12.5 升级到 1.13.2 时,我看到了提到的错误(未解决的参考:每个和其他)。我可以通过添加对mockk-jvm的依赖项(而不是现有的mockk依赖项或在现有的mockk依赖项旁边)来解决这个问题。

        <dependency>
            <groupId>io.mockk</groupId>
            <artifactId>mockk-jvm</artifactId>
            <version>${mockk.version}</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

该问题之前已得到解决(请参阅https://github.com/mockk/mockk/issues/889)并假设已解决。显然,并非所有情况都是如此。