Press "Enter" to skip to content

REST Assured in Kotlin

REST Assured is a library for testing HTTP/REST based services on the JVM and version 4.1.0 made this experience even better for Kotlin developers by introducing a new Kotlin API. This blog-post will briefly introduce the new API and hopefully convince you that it’s preferable to the Java API if you’re using Kotlin. But first let’s look at a simple example.

Example Resource

We’re going to keep things simple and just assume we have a webserver running at http://localhost:8080/ that upon receiving a GET to /users?deleted=false will return the following JSON response (copied from

Validation with the Java API

Now let’s suppose we want to validate a couple of things:

  1. That the status code is equal to 200
  2. That there are 3 users in the list
  3. That one of the users has a name of “Ervin Howell”
  4. The user with username “Samantha” is employed at “Romaguera-Jacobson”

and then we’d like to return the cities of all users. We can do this quite nicely with the Java API:

While this works fine there are a few annoyances:


The first one has to do with the way reporting of validation errors works. Since REST Assured can’t know which body statement that “terminates” the operation it can only perform, and thus report, validations errors one by one. For example if body("size()", is(3)) fails REST Assured cannot continue and check the two remaining statements (body("name.any { it == 'Ervin Howell' }", is(true)) and body("find { user -> user.username == 'Antonette' }", equalTo("Romaguera-Jacobson"))) since it doesn’t know about these statements ahead of time. You’d have to fix the first statement and then re-run the test to know whether or not the subsequent statements pass or not. For this reason REST Assured has a concept called “multi-body expectations” which let’s you define multiple expectations within a single body statement:

This fixes the problem described above, now all body expectations will be validated in one go and all errors will be reported at once. However if any of the statements preceding the call to body were to fail (statusCode(200) in this case) then body would not be executed since the test has already failed. In a trivial example like this it might not matter all that much, but image a different scenario where you’re also expecting contentType and headers then you can probably tell this is not optimal.


Another annoying thing is that the Java DSL doesn’t work well with the default formatting settings in your IDE. If I press cmd+option+l to format the code in Intellij it would look like this:

Which is much less readable. To work around this I often select the code, press cmd+option+t and then F to disable formatting. Intellij will then insert hints that prevents the code from being formatted in the future:

Validation with the Kotlin API

By adding the Kotlin Extensions Module to the classpath you can start using the API by importing Given from the io.restassured.module.kotlin.extensions package. The test can then be rewritten as:

It looks similar enough to the Java API but it solves both issues presented earlier. Since the API uses Kotlin’s type-safe builders the formatting problem goes away since Intellij natively understands and formats these builders in a nice way. Also, since all expectations (statusCode and body) are defined inside the Then block, REST Assured can run through all of them in the same go and report all errors at once. This can save you quite a lot of time since you typically use REST Assured for integration testing and the environment (such as Spring) may take some time to boot.

Why capital letters?

You may be wondering why the methods are starting with a capital letter (Given, When, Then etc)? The reason is that when is a reserved keyword in Kotlin and thus it has to be escaped when using it in code (i.e. `when`). To avoid this, the When extension function was introduced. To make things consistent the other methods was thus also named with the first letter capitalized. I’d love the hear some comments on this decision though, does it make sense or not?


As you’ve seen, the Kotlin API can help to solve two common annoyances that you may experience with the Java API. For this reason, the Kotlin API is recommended for Kotlin users. Any feedback, for example on the naming of methods, is highly appreciated. Thanks for reading!

Be First to Comment

Leave a Reply

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