Kotlin
Avoid escaping “when” keyword
Section titled “Avoid escaping “when” keyword”Kotlin is a language developed by JetBrains and it integrates very well with Java and REST Assured. When using it with REST Assured there’s one thing that can be a bit annoying. That is you have to escape when since it’s a reserved keyword in Kotlin. You can do this either by using Kotlin Extension Module (recommended) or you can simply create your own extension method (the approach shown below). For example:
@Testfun `kotlin rest assured example`() { given(). param("firstName", "Johan"). param("lastName", "Haleby"). `when`(). get("/greeting"). then(). statusCode(200). body("greeting.firstName", equalTo("Johan")). body("greeting.lastName", equalTo("Haleby"))}To get around this, create an extension function that creates an alias to when called When:
fun RequestSpecification.When(): RequestSpecification { return this.`when`()}The code can now be written like this:
@Testfun `kotlin rest assured example`() { given(). param("firstName", "Johan"). param("lastName", "Haleby"). When(). get("/greeting"). then(). statusCode(200). body("greeting.firstName", equalTo("Johan")). body("greeting.lastName", equalTo("Haleby"))}Notice that we don’t need any escaping anymore. For more details refer to this blog post.
Kotlin extension module
Section titled “Kotlin extension module”The kotlin-extensions module provides some useful extension functions when working with REST Assured from Kotlin. First, you need to add the module to the project:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>kotlin-extensions</artifactId> <version>7.0.0-RC1</version> <scope>test</scope></dependency>and then import Given from the io.restassured.module.kotlin.extensions package. You can then use it like this:
val message: String =Given { port(7000) header("Header", "Header") body("hello")} When { put("/the/path")} Then { statusCode(200) body("message", equalTo("Another World"))} Extract { path("message")}Besides a more pleasing API for Kotlin developers it also has a couple of major benefits to the Java API:
- All failed expectations are reported at the same time
- Formatting the code in your IDE won’t mess up indentation
Since REST Assured 7.0.0, Given(client) { } starts a request with the settings of a RestAssuredClient instead of the static settings, for example in tests that run in parallel or for a second service:
val orders = RestAssuredClient.builder().port(8081).build()
Given(orders) { queryParam("status", "open")} When { get("/orders")} Then { statusCode(200)}Note that the names of the extension functions are subject to change in the future (although it’s probably not likely). You can read more about the rationale and benefits of the Kotlin API in this blog post.
Kotlin extension module for Spring MockMvc
Section titled “Kotlin extension module for Spring MockMvc”There are Kotlin extensions for the Spring MockMvc module too, so you can write tests like this:
class RestAssuredMockMvcKotlinExtensionsTest {
@Test fun example() { val mockMvc = MockMvcBuilders.standaloneSetup(GreetingController()) .build()
val id: Int = Given { mockMvc(mockMvc) param("name", "Johan") } When { get("/greeting") } Then { body( "id", Matchers.equalTo(1), "content", Matchers.equalTo("Hello, Johan!") ) } Extract { path("id") }
assertThat(id).isEqualTo(1)}To use it depend on:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>spring-mock-mvc-kotlin-extensions</artifactId> <version>7.0.0-RC1</version> <scope>test</scope></dependency>and import the extension functions from the io.restassured.module.mockmvc.kotlin.extensions package.
Since REST Assured 7.0.0, Given(client) { } starts a request with the settings of a RestAssuredMockMvcClient instead of the static settings (see Parallel tests with RestAssuredMockMvc):
val client = RestAssuredMockMvcClient.builder().standaloneSetup(GreetingController()).build()
Given(client) { param("name", "Johan")} When { get("/greeting")} Then { body("content", Matchers.equalTo("Hello, Johan!"))}Kotlin extension module for Spring WebTest
Section titled “Kotlin extension module for Spring WebTest”REST Assured 5.4.0 introduced Kotlin extension support for the Spring WebTest module. This allows one to write tests like this:
class RestAssuredWebTestClientKotlinExtensionsTest {
val webTestClient = WebTestClient .bindToController(GreetingController()) .build()
val id: Int = Given { webTestClient(webTestClient) param("name", "Johan") } When { get("/greeting") } Then { body( "id", Matchers.equalTo(1), "content", Matchers.equalTo("Hello, Johan!") ) } Extract { path("id") }
assertThat(id).isEqualTo(1)}To use it, depend on:
<dependency> <groupId>io.rest-assured</groupId> <artifactId>spring-web-test-client-kotlin-extensions</artifactId> <version>7.0.0-RC1</version> <scope>test</scope></dependency>and import the extension functions from the io.restassured.module.webtestclient.kotlin.extensions package.
Since REST Assured 7.0.0, Given(client) { } starts a request with the settings of a RestAssuredWebTestClientClient instead of the static settings (see Parallel tests with RestAssuredWebTestClient):
val client = RestAssuredWebTestClientClient.builder().webTestClient(webTestClient).build()
Given(client) { param("name", "Johan")} When { get("/greeting")} Then { body("content", Matchers.equalTo("Hello, Johan!"))}
