37

I have written some basic unit tests with Kotlin and Junit 5. Unfortunately, when I run them from Intellij IDEA, the tests are not found by Gradle. I am getting the message "No tests found for given includes: [de.mhaug.phd.btcwallet.BasicTests]" from Gradle and the message "Test event not received" from Intellij.

Interestingly, running it from the commandline with "./gradlew :clean :test" reports a successful build. However, my first test is obviously red, so this shows that Gradle did not execute it.

I already tried running it with more verbose output but nothing helpful showed up. Here is a minimal (not) working example:

package de.mhaug.phd.btcwallet

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Test

class BasicTests {
  @Test
  fun hey() {
    assertEquals(3,1+1)
  }

  @Test
  fun hey2() {
    assertFalse(3==1+1)
  }
}

This is my build.gradle:

buildscript {
    ext.kotlin_version = '1.2.10'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

group 'de.mhaug.phd'
version '1.0-SNAPSHOT'

apply plugin: 'kotlin'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.2'
    testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.2'
//    testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: ''
//    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.2'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

How can I make Intellij/Gradle execute my tests?

icehawk
  • 1,145
  • 3
  • 12
  • 21
  • 28
    http://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle – JB Nizet Dec 25 '17 at 14:14
  • Possible duplicate of [How to use JUnit 5 with Gradle?](https://stackoverflow.com/questions/44429751/how-to-use-junit-5-with-gradle) – JB Nizet Dec 25 '17 at 14:19
  • @JBNizet Thank you. My tests are executed by Gradle, now. However, Intellij still displays the message "test events were not found", although the ests are visible in the command line. – icehawk Dec 25 '17 at 14:40
  • IntelliJ can execute JUnit tests 5 directly. But if you configure it to delegate to gradle, than it can't work, because it delegates to the standard test task, and the test task doesn't support JUnit 5 yet. – JB Nizet Dec 25 '17 at 14:55
  • I am using intellij 2020.2 and whilst running a class worked fine, I needed to gradle refresh (and possibly even open build.gradle.kts to trigger indexing) to pass individual tests – jMan Nov 10 '20 at 20:41

5 Answers5

39

No, the first comment of @JB Nizet has pointed it out: add test.useJUnitPlatform() alongside your JUnit 5 dependencies. It is the way.

http://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle

test {
    useJUnitPlatform()
}

or:

dependency {
    ...
    // junit 5
    testImplementation ('org.junit.jupiter:junit-jupiter-api')
    testCompile ('org.junit.jupiter:junit-jupiter-params')
    testRuntime ('org.junit.jupiter:junit-jupiter-engine')
    testCompile ('org.mockito:mockito-junit-jupiter')
    test.useJUnitPlatform() // fix "test events not received" bug in IDEA
}

Upvote his comment, not this answer!

WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • 4
    How to add `test.useJUnitPlatform()` when using Kotlin syntax for gradle? – xetra11 Jan 09 '20 at 11:24
  • I don't know kotlin, but it should not matter because the line should be added in `build.gradle` so it should applies, too? – WesternGun Jan 09 '20 at 13:50
  • 10
    in Kotlin, i could get it working with `tasks.test { useJUnitPlatform() }` (at the outermost scope). – Vibin May 18 '20 at 08:02
  • 4
    In my case, I had to also add: `testCompile 'org.junit.jupiter:junit-jupiter-engine:5.6.0'` in the dependencies section – Nether May 31 '20 at 06:54
  • Another cause could be that the structure in your `test`/`itest` folder is not the same as in `main`. Once I created some integration tests and put them under `src`, but I forgot to add `java` level, as in `main`; I just put `com.example.xxx` and that fails all itests. – WesternGun Jul 08 '20 at 17:12
2

I think your particular issue of tests not being run was or would be solved by updated everything to the latest version (Gradle, Kotlin, IntelliJ, etc.). Everything is moving really fast at the moment so keeping up to date is a must.

About the 'test events were not received'

As far as I know this is sort of expected. The point is, when using Gradle tasks, either trust your tests were run or figure out a way to display per-test logging.

You need to make a distinction when 'executing the gradle task' or 'running tests using IntelliJ' (e.g. by clicking the gutter icon next to a test).

Running from IntelliJ.

IntelliJ integrates wonderfully with Gradle, in the sense that if you run from IntelliJ, then it will not only run the tests (via Gradle?) but also show some nice test results. All works well with JUnit 5, Kotlintest, Spek, using normal Gradle or Gradle Kotlin DSL or whatever you use.

Shameless self-advert: JUnit 5 and Gradle (also shows Gradle test logging): How to use JUnit 5 with Gradle? and JUnit 5, Kotlintest and Gradle Kotlin DSL: How to run kotlintest tests using the Gradle Kotlin DSL?

Running the Gradle task.

However, when you run a Gradle task using the Gradle tool window, you will see as you mentioned 'test events were not received'. I think this is because you just run a Gradle task which ran the test, and that does not have anything to do with IntelliJ per se, it doesn't integrate in that it reports back results - it just prints to the command line.

Compare for example with running the Kotlin. When you use the Gradle task, it will output in build/. But when you run the main method using the gutter icon, so using IntelliJ, it will output in out/. These are two different things running.

PHPirate
  • 7,023
  • 7
  • 48
  • 84
  • 1
    I have run into several issues (for instance https://stackoverflow.com/questions/48544015/intellij-idea-2017-3-unable-to-start-kotlin-spring-boot-app-configuration-cla) regarding what PHPirate says. I'd also advice upgrading to the latest Kotlin version both in IntelliJ and build.gradle (1.2.31) – codependent Apr 19 '18 at 11:45
2

Be careful when writing your tests as expression-body functions: their return type should still remain Unit. Example:

import org.assertj.core.api.Assertions.assertThat
import kotlinx.coroutines.runBlocking

// ...

    @Test
    fun t() = runBlocking {
        assertThat(42).isEqualTo(42)
    }

The return type of such test method will be AbstractIntegerAssert<*> and not Unit, so it will be still recognized as a test by IDEA but not by the JUnit engine (when invoked via gradle test).

Correct example:

    @Test
    fun t() = runBlocking<Unit> {
        assertThat(42).isEqualTo(42)
    }
Bass
  • 4,977
  • 2
  • 36
  • 82
0

I faced the same issue with Java 11 and JUnit 5. It started working for me as soon as I made the class public.

Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46
0

I needed to add

        testImplementation(platform("org.junit:junit-bom:5.7.0"))

as well. In sum

        // junit 5
        testImplementation(platform("org.junit:junit-bom:5.7.0"))
        testImplementation('org.junit.jupiter:junit-jupiter-api')
        testCompile('org.junit.jupiter:junit-jupiter-params')
        testRuntime('org.junit.jupiter:junit-jupiter-engine')
        testCompile group: 'org.mockito', name: 'mockito-inline', version: '3.2.0'
        test.useJUnitPlatform() // fix "test events not received" bug in IDEA
        testCompile 'com.willowtreeapps.assertk:assertk-jvm:0.23'