How do you run unit tests?


#1

I'm poking around with Kotlin, but I don't actually know how to run unit tests. I see some outdated examples on writing tests, but I don't know how to run them. I'd like to learn how both for JVM and JavaScript projects.

In a Java project with IntelliJ if I have junit on my classpath I can press ctrl+shift+T to automatically create a test case, but in Kotlin I don’t seem to be able to do that.

Thanks!


#2

I figured it out for Kotlin in the JVM:

  1. add junit as a maven dependency
  2. add the import:  import org.junit.Test


    import kotlin.test.assertEquals
    import org.junit.Test

public class HelloTest {

     Test fun helloTest(): Unit {
          val h = HelloKotlin();
          var hello = h.helloKotlin();
          assertEquals(“hello”, hello);
     }
}


I haven’t figured it out for javascript yet, I don’t know what library I need to import for javascript.
I get the runtime error:

java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
     at java.lang.Class.getDeclaredMethods0(Native Method)
     at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
     at java.lang.Class.getMethod0(Class.java:2764)
     at java.lang.Class.getMethod(Class.java:1653)
     at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
     at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)


#3

Could You please share your project?


#4

For posterity, my questions were answered in the other thread: https://devnet.jetbrains.com/thread/458624?tstart=0