kotlin - Kotest 设置问题, 'unresolved reference'

标签 kotlin gradle kotlintest kotest

我正在尝试将 kotest 添加到我的库中,但我刚刚得到:

e: /home/brett/git/ttnt/src/test/kotlin/io/bmb/ttnt/lib/SqliteManagerTest.kt: (3, 18): Unresolved reference: specs
e: /home/brett/git/ttnt/src/test/kotlin/io/bmb/ttnt/lib/SqliteManagerTest.kt: (4, 18): Unresolved reference: shouldBe
e: /home/brett/git/ttnt/src/test/kotlin/io/bmb/ttnt/lib/SqliteManagerTest.kt: (6, 27): Unresolved reference: StringSpec
e: /home/brett/git/ttnt/src/test/kotlin/io/bmb/ttnt/lib/SqliteManagerTest.kt: (8, 9): Expression '"canary test should pass"' of type 'String' cannot be invoked as a function. The function 'invoke()' is not found
e: /home/brett/git/ttnt/src/test/kotlin/io/bmb/ttnt/lib/SqliteManagerTest.kt: (9, 18): Unresolved reference: shouldBe

我一定把 build.gradle.kts 中的某个地方弄乱了,有些东西没有加载,或者 useJUnitPlatform() 没有运行,但我不确定在哪里。谁能发现我哪里出了问题吗?

src/test/kotlin/io/bmb/ttnt/lib/SqliteManagerTest.kt

package io.bmb.ttnt.lib

import io.kotest.specs.StringSpec
import io.kotest.shouldBe

class SqliteManagerTest : StringSpec() {
    init {
        "canary test should pass" {
            true shouldBe true
        }
    }
}

build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("com.github.jengelman.gradle.plugins:shadow:2.0.4")
    }
}

plugins {
    idea
    id("org.jetbrains.kotlin.jvm") version "1.3.71"
    id("org.jetbrains.kotlin.kapt") version "1.3.72"
    jacoco
    id("com.github.johnrengelman.shadow") version "5.2.0"
}



group = "io.bmb.ttnt"
version = "0.1-ALPHA"

repositories {
    google()
    mavenCentral()
    jcenter()
    maven {
        name="papermc"
        url=uri("https://papermc.io/repo/repository/maven-public/")
    }
    maven {
        url=uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots")
    }
    maven {
        url=uri("https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc")
    }
}


dependencies {
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    compileOnly("com.destroystokyo.paper:paper-api:1.14.4-R0.1-SNAPSHOT")
    compile("io.papermc:paperlib:1.0.3")

    compile(group="org.xerial", name="sqlite-jdbc", version="3.28.0")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7")
    testCompile("io.kotest:kotest-runner-junit5-jvm:4.0.6") // for kotest framework
    testCompile("io.kotest:kotest-assertions-core-jvm:4.0.6") // for kotest core jvm assertions
    testCompile("io.kotest:kotest-property-jvm:4.0.6") // for kotest property test
    testImplementation("io.mockk:mockk:1.10.0")
}

tasks.withType<ShadowJar> {
    baseName = "ttnt"
    classifier = "ALPHA"
    version = "0.1"
}

tasks.test {
    finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
}

tasks.jacocoTestReport {
    dependsOn(tasks.test) // tests are required to run before generating the report
    reports {
        xml.isEnabled = false
        csv.isEnabled = false
        html.destination = file("${buildDir}/jacocoHtml")
    }
}

jacoco {
    toolVersion = "0.8.5"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

idea {
    module {
        isDownloadJavadoc = true
        isDownloadSources = true
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

val compileKotlin: KotlinCompile by tasks
val compileTestKotlin: KotlinCompile by tasks

compileKotlin.kotlinOptions.jvmTarget = "1.8"
compileTestKotlin.kotlinOptions.jvmTarget = "1.8"

defaultTasks("clean", "test", "jacocoTestReport")

更新:

如果我使用的话,尽管会出现弃用警告,但这是有效的:

import io.kotlintest.specs.StringSpec
import io.kotlintest.shouldBe

虽然 io.kotest 是我在 build.gradle.kts 文件中使用的?

最佳答案

StringSpec 现在位于 io.kotest.core.spec.style 包中,而不是 io.kotest.specs

shouldBe 现在位于 io.kotest.matchers 包中,而不是 io.kotest 中。

如果您从代码中删除这两行,IntelliJ 可能会建议使用新的导入,但您的代码将如下所示:

package io.bmb.ttnt.lib

import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class SqliteManagerTest : StringSpec() {
    init {
        "canary test should pass" {
            true shouldBe true
        }
    }
}

关于kotlin - Kotest 设置问题, 'unresolved reference',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62154872/

相关文章:

Kotlin 测试 : How to test for a specific type like: "is y instance of X"

gradle - 如何使用 Kotlintest 和 Gradle 获得分层测试报告

android - 如何让 MockWebServer 工作?

android - 错误 JSON.simple : java. util.zip.ZipException : duplicate entry: org/hamcrest/BaseDescription. 类

kotlin - Android - Glide ".placeholder"方法无法识别

gradle - 启动一个 Gradle 守护进程,1 个忙碌和 6 个停止的守护进程无法重用,使用 --status 获取详细信息

eclipse - 找出哪些更改的资源导致使用Buildship在Eclipse中刷新/重建工作区

KotlinTest 的 BehaviorSpec "No runnable methods"与 SpringJUnit4ClassRunner

java - 为什么 Kotlin 进行空检查,当反编译为 Java 时会声明一些未使用的变量?

可见性降低的 Kotlin/Native c 互操作