Press "Enter" to skip to content

REST Assured with Kotlin

REST Assured is an easy to use library for the JVM that allows you to test REST/HTTP based services. It has good support for Java, Groovy and Scala and in this blog post I want to share some small tips to make the Kotlin integration even smoother.

Kotlin is a pretty interesting language developed by JetBrains that will look familiar if you’re used Java, Groovy and Scala. Let’s pretend that we want to test the following resource written in Sinatra:

If we invoke this service with for example curl:

it’ll return the following JSON:

Writing a simple test case for this service in Kotlin is straight forward:

However you may notice that we have to escape the when function because when is a reserved keyword in Kotlin. So what we’re going to do is to add an extension function called When that we can use as an alias to avoid manual escaping. We can do this by for example defining a trait like this:

Now that we’ve added the alias we import the trait and use it like this:

Voila! Now we don’t need to escape when anymore and the code looks nicer. Thanks to Jasper Blues for bringing this to attention.

One Comment

  1. Bruce Bruce May 18, 2017

    Hey,

    Just to let you know traits keyword is redundant in this example. It is now interface

Leave a Reply

Your email address will not be published. Required fields are marked *