mockk: Bug: Kotlin multiplatform project fails to build with mockk dependency

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Current Behavior

I’m working on a kotlin multiplatform project, when I try to write some unit test by using MockK, I got the Unresolved reference: xxx error when I run the ./gradlew :common:build, don’t actually know this case relates to this issue(#58 ) or not.

...

> Task :common:linkTestDebugExecutableIos FAILED
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (3, 8): Unresolved reference: io
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (4, 8): Unresolved reference: io
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (5, 8): Unresolved reference: io
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (11, 4): Unresolved reference: MockK
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (18, 5): Unresolved reference: MockKAnnotations
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (26, 5): Unresolved reference: verify
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (40, 5): Unresolved reference: verify
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (52, 5): Unresolved reference: verify
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (59, 5): Unresolved reference: verify
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (66, 5): Unresolved reference: verify
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (73, 5): Unresolved reference: verify
e: xxx/accounting-multiplatform/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt: (80, 5): Unresolved reference: verify
8 actionable tasks: 8 executed
19:05:52: Task execution finished 'build'.


Steps to Reproduce

You can clone the project https://github.com/littleGnAl/accounting-multiplatform/tree/littlegnal/common-test.

Uncomment the https://github.com/littleGnAl/accounting-multiplatform/blob/littlegnal/common-test/android/common/src/commonTest/kotlin/com/littlegnal/accountingmultiplatform/data/SqlDelightManagerTest.kt.

Then run the ./gradlew :common:build to reproduce

Context

My gradle configuration is like below:

apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'com.squareup.sqldelight'
apply plugin: 'kotlinx-serialization'

sqldelight {
    AccountingDB {
        packageName = "com.littlegnal.accountingmultiplatform"
    }
}

kotlin {
    sourceSets {
        commonMain {
            dependencies {
                implementation deps.kotlin.stdlib.stdlib
                implementation deps.kotlin.serialiaztion.runtime.common
                implementation deps.kotlin.coroutines.common
            }
        }

        commonTest {
            dependencies {
                implementation deps.kotlin.coroutines.core
                implementation deps.kotlin.coroutines.common
                implementation deps.kotlin.coroutines.test
                implementation deps.kotlin.test.common
                implementation deps.kotlin.test.annotations
//                implementation deps.mockk.common
//                implementation deps.mockk.mockk
                implementation "io.mockk:mockk-common:1.9.3"
                implementation "io.mockk:mockk:1.9.3"

            }
        }

        androidTest {
            dependencies {
                implementation deps.kotlin.test.test
                implementation deps.kotlin.test.junit
            }
        }

        androidMain {
            dependencies {
                implementation deps.kotlin.stdlib.stdlib
                implementation deps.sqldelight.runtimejvm
                implementation deps.kotlin.serialiaztion.runtime.runtime
                implementation deps.kotlin.coroutines.android
            }
        }

        iosMain {
            dependencies {
                implementation deps.kotlin.stdlib.stdlib
                implementation deps.sqldelight.driver.ios
                implementation deps.kotlin.serialiaztion.runtime.native
                implementation deps.kotlin.coroutines.native
            }
        }

        iosTest {
            dependencies {
                implementation "io.mockk:mockk-common:1.9.3"
                implementation "io.mockk:mockk:1.9.3"
            }
        }
    }

    targets {
        fromPreset(presets.jvm, 'android')
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
                              ? presets.iosArm64 : presets.iosX64

        fromPreset(iOSTarget, 'ios') {
            binaries {
                framework('common')
            }
        }
    }
}

// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
    compileClasspath
}

task packForXCode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
    final def framework = kotlin.targets.ios.binaries.getFramework("common", mode)

    inputs.property "mode", mode
    dependsOn framework.linkTask

    from { framework.outputFile.parentFile }
    into frameworkDir

    doLast {
        new File(frameworkDir, 'gradlew').with {
            text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
            setExecutable(true)
        }
    }
}

task iosTest {
    def device = project.findProperty("iosDevice")?.toString() ?: "iPhone 8"
    dependsOn 'linkTestDebugExecutableIos'
    group = JavaBasePlugin.VERIFICATION_GROUP
    description = "Runs tests for target 'ios' on an iOS simulator"

    doLast {
        def binary = kotlin.targets.ios.compilations.test.getBinary('EXECUTABLE', 'DEBUG')
        exec {
            commandLine 'xcrun', 'simctl', 'spawn', device, binary.absolutePath
        }
    }
}

tasks.build.dependsOn packForXCode
  • MockK version: 1.9.3
  • OS: macOS Mojave 10.14.4
  • Kotlin version: 1.3.31
  • Type of test: unit test OR android instrumented test: Unit test

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 17
  • Comments: 29

Most upvoted comments

I think basically mockk has no support for Kotlin Multiplatform so it is not possible to use this library with it.

probably soon it could be supported

Almost 3 years, how soon? LOL

I do not know how but after appliying this, now my files see mockk dependency and kmm project structure also not broken:

   val commonTest by getting {
        dependencies {
            implementation(kotlin("test-common"))
            implementation(kotlin("test-annotations-common"))
            implementation("io.mockk:mockk-common:1.11.0")
        }
    }

    val androidTest by getting {
        dependencies {
            implementation(kotlin("test"))
            implementation(kotlin("test-junit"))
            implementation("junit:junit:4.13.2")
            implementation("io.mockk:mockk:1.11.0")

        }
    }

Probably found the workaround. Instead of just adding: implementation("io.mockk:mockk-common:$mockkVersion") add also: implementation("io.mockk:mockk:$mockkVersion")

Works like a charm for my project

Hello. Same here. Any update on this?

Unfortunately the solution of adding implementation("io.mockk:mockk:$mockkVersion") breaks the gradle project sync, see #513.

See @YKakdas’s answer. It works

@silverhammermba yes, I am! so indeed, your need cannot be accomplished by mockk.

Same issue Kotlin version: 1.3.50

Thanks for the report