-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Kotlin
- Avoid Escaping "when" Keyword
- Kotlin Extension Module
- Kotlin Extension Module for Spring MockMvc
- Kotlin Extension Module for Spring WebTest
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:
@Test
fun `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:
@Test
fun `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.
REST Assured 4.1.0 introduced a new module called "kotlin-extensions". This 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>5.5.0</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
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.
REST Assured 4.1.0 introduced Kotlin extension support for the Spring MockMvc module. This allows one to 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>5.5.0</version>
<scope>test</scope>
</dependency>
and import the extension functions from the io.restassured.module.mockmvc.kotlin.extensions
package.
REST Assured 5.5.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>5.5.0</version>
<scope>test</scope>
</dependency>
and import the extension functions from the io.restassured.module.webtestclient.kotlin.extensions
package.