I'm writing game using Kotlin and LibGDX framework. I'm new to testing. I have passed some basic tutorial how to create simple test. And how to configure gradle. I just clicked on class and choose create test.
But, when i try to build project i get an error:
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 looks like this:
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)
}
}
I guess that gradle doesn't see junit, but i followed all instructions. Maybe i missed something.
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"
}
I've configured junit tests for libGdx+kotlin by following steps:
Create 'test' folder in the core project folder - it will be the root folder for test code files: [project-root]/core/test
Add junit dependencies in project main gradle.build file to the project(":core") section:
project(":core") {
....
dependencies {
...
testCompile 'junit:junit:4.12'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
}
}
Add test source set in [project-root]/core/build.gradle file, right under the 'sourceSets.main.java.srcDirs = [ "src/" ]' line:
sourceSets.test.java.srcDirs = ["test/"]
Now the [project-root]/core/test folder will be highlighted with green, which means that this folder is recognized as test source directory. Now you can place there a .kt file with simple junut test, for example:
import org.junit.Test
import kotlin.test.assertEquals
class SimpleTest{
@Test
fun testEquals(){
var b=true
assertEquals(true,b)
}
}
In my case problem was I didn't import
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
Update May 2020: This should be placed in build.gradle's dependencies { }
. As of Android Studio version 4 and current gradle version, the variable should be named $version_kotlin for gradle to properly re-sync.
in app module's build.gradle
replace testImplementation
with
androidTestImplementation
First make sure that the kotlin version defined in build.gradle.kts (project)
is the same as the selected in the compiler.
plugins {
kotlin("jvm") version "1.4.21" apply false
}
If these versions are matting and still not working then proceed with the following steps:
Delete all cache folders including:
Invalidate Caches / Restart...
Open any gradle file (settings.gradle.kts)
In the IDE at the top right click in Link Gradle project
tasks.test {
useJUnitPlatform()
}
Note: This behavior can be defined by gradle or from the IDE
It is very likely that if it worked and you have the dependencies it is a cache problem or gradle configuration.
GL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With