Skip to Content

Expressive Kotlin Unit Test Names

Make your Kotlin unit tests names more readable and expressive

Posted on

When writing in Kotlin, it’s worth noting that it has a different set of keywords than Java does. For example, it is legal to call methods is or object in Java, but not Kotlin. This presents some issues with Java interop, and in order to get around this Kotlin lets you surround those words with backticks so they don’t cause the compiler to yell at you.

For example, if you are in Kotlin and calling a Java method called is, you have to escape it like this:

val javaObject = SomeJavaObject()

Now that we know that’s legal, we can use this to make our method names more expressive. Personally, I feel calling methods like that is more trouble than it’s worth and dirties the code, but in a unit test the build or your IDE is calling those methods for you so we might as well be expressive!

Here’s a small example of this in Kotlin. I used AssertJ and JUnit, but use whatever tools you enjoy.

class `Some Example Tests` {

    fun `Basic addition works properly`() {
        assertThat(1 + 1).isEqualTo(2)
        // ...

    fun `Wow! You can name your methods anything!`() {
        // ...


And the result looks nice in my IDE too:

Test Results in IntelliJ

As you can see I also did the same with the class name just to show it can be done, but I don’t do that in practice. Having used this on a larger project I can confirm that this does look a lot better when running many tests at once or as part of an automated build.

Have fun!