3

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)
            }
        }
Sunbey13
  • 339
  • 4
  • 12
  • Can you share mockk dependency part of your common/build.gradle? Did you look at `Installation` section in https://mockk.io ? – ocos Sep 21 '22 at 11:29
  • 1
    Add also `implementation("io.mockk:mockk:${Version.mockk}")` to your dependencies. Hope this solves your problem. Please check also Kevin's answer to understand why your IDE does not show errors in your editor. – ocos Sep 21 '22 at 17:23
  • but when i remove `implementation(ShareTestDependencies.mockk)` IDE shows errors – Sunbey13 Sep 22 '22 at 07:19
  • @ocos `implementation("io.mockk:mockk:${Version.mockk}")` helped me. Thx a lot – Sunbey13 Sep 22 '22 at 08:39

2 Answers2

5

I see the mentioned errors (Unresolved reference: every and other) when upgrading from mockk 1.12.5 to 1.13.2. I can work around it by adding a dependency to mockk-jvm (instead of or next to the existing mockk dependency).

        <dependency>
            <groupId>io.mockk</groupId>
            <artifactId>mockk-jvm</artifactId>
            <version>${mockk.version}</version>
        </dependency>

The problem was addressed earlier (see https://github.com/mockk/mockk/issues/889) and assumed to be solved. Apparently this is not yet so in all cases.

Steven Kuypers
  • 451
  • 4
  • 6
4

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.

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62