Unresolved reference: mockk

Viewed 49

I have imported mockk library in commonTest -> shared module. There are no import errors in the test classes, but when I run the test I get errors like:

Unresolved reference: every
Unresolved reference: mockk
Unresolved reference: verify

in all places where i use mock library methods. What could be the reason for the errors?

example of my test with errors in console:

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
        }
    }

    ...
}

UPDATE Dependencies

const val mockk = "1.12.5"

const val mockk = "io.mockk:mockk-common:${Version.mockk}"
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
                implementation(ShareTestDependencies.mockk)
                implementation(ShareTestDependencies.coroutinesTest)
            }
        }
1 Answers

If you are using KMP to build native targets (iOS, Linux, Windows, etc), mockk doesn't support native. It does support JVM and JS, and has a common source definition to support those 2 platforms. The IDE may not have an error because there is a common mockk definition (although I've never tried that).

Look at the test task that's actually being run. If it's for a native target, that's definitely what's happening.

Related