Writing tests with Kotlin


Any tips on how to write tests ( preferrably JUnit ) with Kotlin?

As far as I can tell Kotlin doesn’t support annotations which precludes conventional JUnit 4 test style with @Test.

Am I missing something? Is there any consensus on the ‘right’ way to do it?




Hi Rob,

Whilst the plugin currently does not detect JUnit-annotated test methods, you can still manually create run configurations for JUnit test classes written in Kotlin.

Hope this helps


Franck Rasolo


The latest nightly builds already detect JUnit tests properly, so you can simply run a JUnit class.

BTW, annotations are already supported to the extend required by JUnit: you can just say

Test fun foo() {…}


Thanks Franck and Andrey.


Incidentally we've recently refactored a bunch of handy Kotlin methods for testing out into a separate kotlin-test.jar in the distro, so by adding the kool.test.* import you can write kool tests (which use JUnit under the covers). e.g. see CollectionTest.kt

Notice things like the assert { }, expect { }, fail { } and todo { } methods which take blocks for example. Here’s the source for the test methods.


Hi James,

Just curious: JUnit and not TestNG?


Yeah, sorry about that Cedric :) - just wanted something simple working inside IDEA & Ant for writing some tests.

We should have a TestNG flavour of the kotlin-test library too; either that or maybe TestNG could include one (then its only 1 jar to add to folks build)? The kotlin-test library its pretty simple so far, just providing a few simple helpers for using assertions & blocks. e.g.

To be honest these most of these methods could all be testing framework agnostic & just throw the JDK’s java.lang.AssertionError’s. The only real reason its using JUnit at all right now was just to make use of those assertEquals methods to generate nice text (& nice IDEA visualisation) of when values are not the same. It’d be pretty easy to refactor the JUnit bit out and have a vanilla JDK, JUnit and TestNG implementation of the ‘assert equals’ stuff. Heck the same library could even look on the classpath to see which kind of Assert classes are available (JUnit or TestNG) and fail down to just using JDK assertions? (Though thats soundling like togging, the common-logging of testing? :slight_smile:

Or do you fancy writing a testng-kotlin library, so all the TestNG stuff can be exposed nicely in a kotlin API? It’d certainly be nice if the APIs had similar shapes; then just a different import statements - so users could more easily switch to use whichever?


BTW notice how right now if using the kotlin-test library, it hides the JUnit dependency - so it'd be easy to switch the kotlin-test library implementation and use TestNG too :)