When trying to execute a very simple Hello World test in Android, using KotlinTest:
class ExampleUnitTest : FreeSpec() {
init {
"Test" {
2 + 2 shouldBe 4
}
}
}
When trying to execute this in IntelliJ, by clicking the green icon, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:45)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.TestExecutionListener
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more
Process finished with exit code 1
I'm using this dependency:
testImplementation "io.kotlintest:kotlintest-runner-junit4:3.1.8"
JUnit 4 is necessary because Android doesn't support JUnit 5 yet.
When executing ./gradlew test
it works correctly, so I assume this is a problem with IntelliJ only?
I'm unsure of what is causing this, but after hours of useless research, I've found a workaround to solve this issue.
It seems to be a bug in IntelliJ IDEA 2018.2.RC, that causes it to execute code using JUnit 5 anyways. It looks like that it does that because it doesn't find any other TestExecutor in the classpath.
A Workaround to this is forcing your UnitTest to be compiled after IntelliJ builds it.
For that, edit your tests configuration:
And add a Gradle task to compileDebugUnitTestKotlin
:
After this, your tests should become green again.