简体   繁体   中英

How to configure JUnit 5 in a Kotlin multiplatform project using Gradle and IntelliJ?

JUnit 4 (working)

The Kotlin multiplatform template in IntelliJ IDEA 2018.2.3 (Community Edition) relies on JUnit 4.12 in build.gradle for the JVM part of the project:

plugins {
    id 'kotlin-platform-jvm' version '1.2.61'
}
repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    expectedBy project(":TestMulti-common")
    testCompile "junit:junit:4.12"
    testCompile "org.jetbrains.kotlin:kotlin-test"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}

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

Using this template, I can add tests to the common part of the project and the tests are recognized by IntelliJ : a 'Run' icon shows up in the margin of the source files and tests can be run through the context menu.

JUnit 5 (tests not properly recognized in IntelliJ)

How can I achieve a similar setup using JUnit 5?

Of note, I am using Gradle 4.10 (some older examples use junit-platform-gradle-plugin which has been deprecated since Gradle 4.6 ). Documentation on how to set this up is outdated and scarce:

When I try to set up the build.gradle for the JVM part of the project based on the JUnit 5 Gradle example , I can run tests using Gradle , but IntelliJ does not seem to recognize my tests .

plugins {
    id 'kotlin-platform-jvm' version '1.2.61'
}
repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    expectedBy project(":TestMulti-common")

    testCompile "org.jetbrains.kotlin:kotlin-test"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit5"

    testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
    testCompile('org.junit.jupiter:junit-jupiter-params:5.3.1')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}

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

Oddly enough, sometimes test results appear in IntelliJ's test results when running gradle test on the JVM project, but when rerunning the tests the message "Test events were not received" shows up. The 'Run' icons in the margin never appear in source files and neither do the test options in the context menu.

Other people seem to have similar issues, but it is unclear whether or not these are the same/related or have been resolved since:

The many different releases, outdated documentation, and similar issues make it hard to find more information about this problem.

This was a bug in IntelliJ as I described in the following YouTrack issue: IntelliJ does not recognize JUnit 5 tests in Kotlin multiplatform project .

In the latest version this is now working. I tried with Kotlin 1.3.71, the IntelliJ plugin 1.3.71-release-IJ2019.3-1, and JUnit 5.6.0.

In my case IntelliJ IDEA upgrade disabled JUnit plugin, so I needed to enable it again (requires IDE restart):

需要启用 IntelliJ IDEA JUNit 插件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM