简体   繁体   中英

Kotlin Multiplatform Gradle unit test not resolving kotlin.test reference

I am trying to test a Kotlin class in my common library for my Kotlin Multiplatform project in Android Studio.

I have had to reconfigure the build.gradle file several times and managed to fix most of the unresolved references, but Gradle still can't find the reference for the @Test annotation, while the editor recognizes that it is from the kotlin.test library.

Here is my test class:

import kotlin.test.*
import kotlinx.serialization.json.*
import Recipe

class RecipeTest {
    @Test
    fun serializeTest() {
        val keys = arrayOf("Dessert", "Cookies", "Cute")
        val ingredients = arrayOf("12 cups sugar", "2 cups flour", "1 bottle warm love")
        val instructions = arrayOf("Sift together in bowl", "Cook however else you see fit!")
        val recipe = Recipe(
            "Macaroons",
            "Morgan",
            "Today",
            "small cookies",
            "1 hour",
            keys,
            "1 dozen macaroons",
            "Dessert",
            "French",
            false,
            ingredients,
            instructions,
            true
        )
        val jsonString = JSON.stringify(Recipe.serializer(), recipe)
        val obj = JSON.parse(Recipe.serializer(), jsonString)
        assertEquals(Recipe.toString(), jsonString)
        assertEquals(Recipe.toString(), obj.toString())
    }
}

And my module build.gradle file:

plugins {
    id("com.android.library")
}

apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'

android {
    compileSdkVersion = 28
    buildToolsVersion = '28.0.3'
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    sourceSets {
        main {
            manifest.srcFile 'src/androidMain/AndroidManifest.xml'
        }
    }
}

kotlin {
    android {

    }
    iosArm64 {
        binaries {
            executable()
        }
    }
    iosX64 {
        binaries {
            executable()
        }
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1'
            }
        }

        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1'
                implementation kotlin('test')
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        androidMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }

        iosMain {

        }
    }
}

configurations {
    compileClasspath
}

When the test in run from command line, the build fails with an exception, saying

Unresolved reference: test

at the line with the @Test annotation.

EDIT: I changed the accepted answer to the one that appears to be most helpful to others, but I'm leaving my old one just in case.

I had a similar issue and found that I needed to explicitly add the platform specific Kotlin test dependencies:

kotlin {

  // ...

  sourceSets {
    commonTest {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-test-annotations-common"
        implementation "org.jetbrains.kotlin:kotlin-test-common"
      }
    }

    jvmTest {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-test-junit"
      }
    }

    jsTest {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-test-js"
      }
    }
  }
}

With only the commonTest dependencies I received the "Unresolved reference: test" error. Once I added the jvmTest and jsTest blocks it fixed the error.

As it turns out, I had some conflicting dependencies in my commonTest source set after all. The 'test' dependency was conflicting with the 'test-common', which led to problems that were buried in some build logs. After deleting the extra dependencies, the build succeeded and the test ran. ( and passed!)

sourceSets {
   ...
   commonTest {
      dependencies {
         //only these are needed
         implementation kotlin('test-common')
         implementation kotlin('test-annotations-common')
      }
   }
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM