5

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?

LeoColman
  • 6,950
  • 7
  • 34
  • 63

3 Answers3

2

I had a similar issue which got resolved when I clicked on the "reload all projects" option

enter image description here

AJC
  • 981
  • 10
  • 17
1

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:

enter image description here

And add a Gradle task to compileDebugUnitTestKotlin:

enter image description here

After this, your tests should become green again.

LeoColman
  • 6,950
  • 7
  • 34
  • 63
1

I have the same problem, and adding app:compileDebugUnitTestKotlin to run configuration didn't work for me. I even replaced Gradle-aware Make with Build to get the same configuration, but it didn't help either.

I also tried to annotate the class with @RunWith(JUnit4::class) since you suggested it was executed with Junit 5. But this also didn't help.

What helped though was to downgrade to 3.1.5. But it's not the best solution, since there are some useful functions in newer versions.

It's good to know it works from command line though. This way I can use the newest version and run tests using the Gradle run configuration for app:test and look at results in the browser. Not perfect, but...

arekolek
  • 9,128
  • 3
  • 58
  • 79
  • 1
    I did find a workaround by downgrading to 3.1.5, And [this was reported to the developer](https://github.com/kotlintest/kotlintest/issues/385) – LeoColman Jul 29 '18 at 01:18
  • But it's very unfortunate that executing compileDebugunitTestKotlin didn't work for you :x – LeoColman Jul 29 '18 at 01:19
  • You can also execute in Terminal -> `./gradlew test` and see the test results it generates (it's an index.html file) in the browser – LeoColman Jul 29 '18 at 01:19