ica*_*bas 6 java junit gradle libgdx kotlin
我正在使用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'.
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
我猜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"
}
Run Code Online (Sandbox Code Playgroud)
就我而言,问题是我没有导入
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
Run Code Online (Sandbox Code Playgroud)
2020 年 5 月更新:这应该放在 build.gradle 中dependencies { }。从 Android Studio 版本 4 和当前的 gradle 版本开始,该变量应命名为 $ version_kotlin以便 gradle 正确重新同步。
我已经通过以下步骤为 libGdx+kotlin 配置了 junit 测试:
在核心项目文件夹中创建“test”文件夹 - 它将是测试代码文件的根文件夹:[project-root]/core/test
将项目主 gradle.build 文件中的 junit 依赖项添加到 project(":core") 部分:
project(":core") {
....
dependencies {
...
testCompile 'junit:junit:4.12'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
}
}
Run Code Online (Sandbox Code Playgroud)在 [project-root]/core/build.gradle 文件中添加测试源集,就在 'sourceSets.main.java.srcDirs = [ "src/" ]' 行下:
sourceSets.test.java.srcDirs = ["test/"]
Run Code Online (Sandbox Code Playgroud)现在 [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)
}
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
5199 次 |
| 最近记录: |