我正在Android Studio中为我的Kotlin多平台项目测试公共库中的一个Kotlin类。
我不得不多次重新配置build.gradle
文件,并设法修复了大部分未解决的引用,但Gradle仍然无法找到@Test
注解的引用,而编辑器识别出它来自kotlin.test
库。
下面是我的测试类:
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())
}
}
字符串
我的模块build.gradle文件:
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
}
型
当测试从命令行运行时,构建失败并出现异常,
未解析参考:测试
在带有@Test
注解的行中。
编辑:我把公认的答案改成了对别人最有帮助的答案,但为了以防万一,我还是留下了我的旧答案。
3条答案
按热度按时间lztngnrs1#
我遇到了类似的问题,发现我需要显式地添加特定于平台的Kotlin测试依赖项:
字符串
只有
commonTest
dependencies
我收到了“Unresolved reference:test”错误。一旦我添加了jvmTest
和jsTest
块,它就修复了错误。jobtbby32#
事实证明,我在commonTest源代码集中毕竟有一些冲突的依赖项。“test”依赖项与“test-common”冲突,这导致了隐藏在一些构建日志中的问题。删除额外的依赖项后,构建成功 *,测试运行 。( 和 * 通过!)
字符串
fzwojiic3#
有一天,我所有的Kotlin多平台测试都因为同一个错误而突然停止工作,尽管我使用的是官方KMM文档中指定的 * Kotlin(“test”)*。但是,按照Google issue tracker上的建议,将 implementation(Kotlin(“test”)) 替换为 implementation(Kotlin(“test-junit”)),这就帮了大忙。