未解析的引用:junit

2022-09-03 06:35:20

我正在使用Kotlin和LibGDX框架编写游戏。我是测试新手。我已经通过了一些关于如何创建简单测试的基本教程。以及如何配置渐变。我刚刚点击类并选择创建测试。

但是,当我尝试构建项目时,我得到一个错误:

e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (1, 12): Unresolved reference: junit
e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (2, 12): Unresolved reference: junit
e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (6, 6): Unresolved reference: Test
e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (8, 9): Unresolved reference: Assertions
e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (11, 6): Unresolved reference: Test
e: /Users/maximternovtsi/bagel/core/src/test/test/BagelTest.kt: (13, 9): Unresolved reference: Assertions

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':core:compileKotlin'.

BagelTest看起来像这样:

import org.junit.jupiter.api.Test

import org.junit.jupiter.api.BeforeEach


internal class BagelTest {


    @BeforeEach
    internal fun setUp() {
    }

    @Test
    internal fun passes() {
        assert(true)
    }
}

我猜gradle没有看到junit,但我遵循了所有说明。也许我错过了一些东西。

   buildscript {
    repositories {

        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'org.multi-os-engine:moe-gradle:1.4.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.51"
    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

    version = '1.0'
    ext {
        appName = "Bagel"
        gdxVersion = '1.9.8'
        junitJupiterVersion  = '5.0.2'
    }

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

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

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
        compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "android"
    apply plugin: "kotlin-android"

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
    }
}

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

    /*kotlin {
        experimental {
            coroutines 'enable'
        }
    }*/

    sourceSets.test.java.srcDirs = ["/test"]

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"
        compile "com.badlogicgames.ashley:ashley:1.7.3"

        testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
//        testCompile "org.mockito:mockito-core:2.2.7"
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

答案 1

我通过以下步骤为libGdx + kotlin配置了junit测试:

  1. 在核心项目文件夹中创建“test”文件夹 - 它将是测试代码文件的根文件夹:[project-root]/core/test

  2. 将项目主 gradle.build 文件中的 junit 依赖项添加到 project(“:core”) 部分:

    project(":core") {
      ....
      dependencies {
        ...
        testCompile 'junit:junit:4.12'
        testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
       }
     }
    
  3. 在 [project-root]/core/build.gradle file 中添加测试源代码集,就在 'sourceSets.main.java.srcDirs = [ “src/” ]“ 行的正下方:

    sourceSets.test.java.srcDirs = ["test/"]
    
  4. 现在,[project-root]/core/test文件夹将以绿色突出显示,这意味着此文件夹被识别为测试源目录。现在,您可以使用简单的 junut 测试放置一个 .kt 文件,例如:

    import org.junit.Test
    import kotlin.test.assertEquals
    
    class SimpleTest{
    
        @Test
        fun testEquals(){
            var b=true
            assertEquals(true,b)
        }
    }
    

答案 2

在我的情况下,问题是我没有导入

androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"

2020年5月更新:这应该放在build.gradle的.从 Android Studio 版本 4 和当前 gradle 版本开始,该变量应命名为 $version_kotlin,以便 gradle 正确重新同步。dependencies { }


推荐