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.
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.
In your Gradle project, in the editor, create or select a test to run. From the context menu, select Run <test name>. icon in the left gutter. If you selected the Choose per test option, IntelliJ IDEA displays both Gradle and JUnit test runners for each test in the editor.
IntelliJ IDEA supports the ability to actually run tests written for JUnit 5 – there's no need to use the additional libraries (like the Gradle or Maven plugins for example), all you need is to include the JUnit 5 dependency.
IntelliJ IDEA works with multiple testing frameworks out of the box, for example, JUnit, Spock, TestNG, or Cucumber..
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.
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