首页 > 解决方案 > 如何在 LibGdx 项目中使用 Gradle 配置 KoTest?当前无法构建,因为无法识别 StringSpec

问题描述

我有一个由 LibGdx 工具设置的标准 LibGdx 项目,仅针对桌面。它使用 Gradle (Groovy DSL) 来管理依赖项和任务。我已将核心模块转换为 Kotlin,并尝试使用 Kotest 添加 Kotlin 测试模块。

我在他们的 GitHub 上遵循了 Gradle 的 Kotest 说明,但编译失败,因为StringSpec没有重新识别(Unresolved reference: StringSpec)。我认为 LibGdx 的默认 Gradle 设置可能有点过时或使用较旧的语法/结构,并且可能与 Kotest 针对较新版本的 Gradle 的说明相冲突?

现在我已经删除了任何测试,只是试图让它识别StringSpec和编译。我什至还没有达到让 IntelliJ 识别和运行测试的阶段。这是我到目前为止所拥有的:

core.tests/tests/com/me/game/AcceptanceTests.kt

package com.jansky.myproject

class AcceptanceTests : StringSpec() {

}

core.tests/gradle.build

version '1.0'

sourceCompatibility = 1.7
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

sourceSets.main.java.srcDirs = [ "tests/" ]

tasks.withType(Test) {
    useJUnitPlatform()
}

eclipse.project.name = appName + "-core.tests"

./build.gradle(即根构建文件)

buildscript {
    ext.kotlinVersion = '1.3.71'

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
        google()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

allprojects {
    apply plugin: "eclipse"

    version = '1.0'
    ext {
        appName = "game"
        gdxVersion = '1.9.10'
        roboVMVersion = '2.3.8'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        google()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":desktop") {
    apply plugin: "kotlin"


    dependencies {
        implementation project(":core")
        api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.ashley:ashley:1.7.3"
        compile group: 'io.github.libktx', name: 'ktx-ashley', version: '1.9.10-b4'
    }
}

project(":core") {
    apply plugin: "kotlin"

    dependencies {
        api "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
        compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
        compile "com.badlogicgames.ashley:ashley:1.7.3"
        compile group: 'io.github.libktx', name: 'ktx-ashley', version: '1.9.10-b4'
    }
}

project(":core.tests") {
    apply plugin: "kotlin"

    test {
        useJUnitPlatform()
    }

    dependencies {
        api "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
        compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
        compile "com.badlogicgames.ashley:ashley:1.7.3"
        compile group: 'io.github.libktx', name: 'ktx-ashley', version: '1.9.10-b4'
        implementation 'io.kotest:kotest-runner-junit5:4.0.2'
        implementation 'io.kotest:kotest-assertions-core:4.0.2'
    }
}

设置.gradle

include 'desktop', 'core', 'core.tests'

Gradle-wrapper.properties

#Sat Apr 04 15:53:20 BST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

我没有太多的JVM经验,所以我有点茫然。希望我错过了一些对更了解 Gradle 的人来说显而易见的东西。有任何想法吗?

标签: gradlekotlinlibgdxkotlintestkotest

解决方案


推荐阅读