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.
The Kotlin/JS Gradle plugin lets you run tests through a variety of test runners that can be specified via the Gradle configuration. In order to make test annotations and functionality available for the JavaScript target, add the correct platform artifact for kotlin.test in build.gradle.kts: Copied!
Sign in to your account produces the error "kotlin: Unresolved reference: test". This happens when editing outside any project, i.e. there are no e.g. Gradle files to analyze for dependencies. As mentioned in #198, this should work as of #154.
Bookmark this question. Show activity on this post. When I compile my kotlin multiplatform project I get Unresolved reference on stuffs that I use in the common module. But the gradle synchronisation works fine. You can found my project here I followed the tutorial from Jetbrain to build a mobile multiplatform project. Everything works fine.
You can tune how tests are executed in Kotlin/JS by adjusting the settings available in the testTask block in the Gradle build script. For example, using the Karma test runner together with a headless instance of Chrome and an instance of Firefox looks like this:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With