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:
- As shown above, the default project template built in IntelliJ does not use JUnit 5.
- The JUnit team provides examples, but not for Kotlin multiplatform.
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:
- Junit 5 and Gradle 4.6 check task shows "Test events were not received"
- junit5 tests are not shown in test view
- No "Run" icon on the left for tests in common part of a multi-platform project
- Kotlin multiplatform projects run common module test in IDEA
The many different releases, outdated documentation, and similar issues make it hard to find more information about this problem.
