22

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"
}
4
  • Are you sure gradle has actually downloaded junit? Try running ./gradlew --refresh-dependencies in the command line to tell it to recheck those dependencies. Commented Dec 19, 2017 at 22:04
  • 1
    Did you try putting BagelTest.kt in /core/src/test/kotlin/? Commented Dec 20, 2017 at 13:04
  • Make sure that your test folder is in the main folder of the project. Commented Feb 20, 2020 at 9:52
  • I am using Android Studio 4.0 version and there is (test) package already in the android project it's a default package , I have put my kotlin test file there and there is no problem with it. I didn't run the file yet but there are no import errors for junit etc. I hope this is helpful. Commented Aug 17, 2020 at 13:41

6 Answers 6

9

I've configured junit tests for libGdx+kotlin by following steps:

  1. Create 'test' folder in the core project folder - it will be the root folder for test code files: [project-root]/core/test

  2. 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"
       }
     }
    
  3. 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/"]
    
  4. 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)
        }
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

I am using Android Studio 4.0 version and there is (test) package already in the android project it's a default package , I have put my kotlin test file there and there is no problem with it. I didn't run the file yet but there are no import errors for junit etc. I hope this is helpful.
testCompile is deprecated, should be replaced with testImplementation
9

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.

Comments

3

in app module's build.gradle

replace testImplementation with androidTestImplementation

Comments

1

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:


  1. Delete all cache folders including:

    • .gradle
    • .idea
  2. Invalidate Caches / Restart...

  3. Open any gradle file (settings.gradle.kts)

  4. In the IDE at the top right click in Link Gradle project

Link Gradle project

  1. Finally, specifies that JUnit 5 should be used to execute the tests

build.gradle.kts

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

1 Comment

Invalidating Caches didn't worked for me, but deleting .gradle, .idea and the build folder. Thank you!
0

You should:

1) remove internal word - it is not required

2) using simple assert method in tests is wrong - use methonds from org.junit.Assert.*

Comments

0

I am using Android Studio 4.0 version and there is (test) package already in the android project it's a default package , I have put my kotlin test file there and there is no problem with it. I didn't run the file yet but there are no import errors for junit etc. I hope this is helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.