java – 未解决的参考:junit

我正在使用Kotlin和LibGDX框架编写游戏.我是新手测试.我已经通过了一些基础教程如何创建简单的测试.以及如何配置gradle.我只是点击了课程并选择了创建测试.

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

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"
}
最佳答案
我已经通过以下步骤为libGdx kotlin配置了junit测试:

>在核心项目文件夹中创建’test’文件夹 – 它将是测试代码文件的根文件夹:[project-root] / core / test
>将项目main gradle.build文件中的junit依赖项添加到项目(“:core”)部分:

project(":core") {
  ....
  dependencies {
    ...
    testCompile 'junit:junit:4.12'
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
   }
 }

>在[source-root] /core/build.gradle文件中添加测试源集,位于’sourceSets.main.java.srcDirs = [“src /”]’行的正下方:

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

>现在[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)
    }
}

转载注明原文:java – 未解决的参考:junit - 代码日志