Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: junit

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"
}
like image 490
icarumbas Avatar asked Dec 18 '17 15:12

icarumbas


4 Answers

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)
        }
    }
    
like image 161
elenatres Avatar answered Nov 20 '22 04:11

elenatres


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.

like image 44
Nicola Gallazzi Avatar answered Nov 20 '22 06:11

Nicola Gallazzi


in app module's build.gradle

replace testImplementation with androidTestImplementation

like image 3
ir2pid Avatar answered Nov 20 '22 06:11

ir2pid


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

like image 1
Braian Coronel Avatar answered Nov 20 '22 04:11

Braian Coronel