Skip to content

Scala

REST Assured 5.5.0 introduced a new module called “scala-extensions” for Scala 3. This module provides some useful extension functions when working with REST Assured from Scala 3. First, you need to add the module to the project:

libraryDependencies += "io.rest-assured" % "scala-extensions" % "7.0.0-RC1"
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>scala-extensions</artifactId>
<version>7.0.0-RC1</version>
<scope>test</scope>
</dependency>
testImplementation 'io.rest-assured:scala-extensions:7.0.0-RC1'

and then import Given from the io.restassured.module.scala.extensions package. You can then use it like this:

Given(req =>
req.port(7000)
req.header("Header", "Header")
req.body("hello")
)
.When(
_.put("/the/path")
)
.Then(res =>
res.statusCode(200)
res.body("message", equalTo("Hello World"))
)

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:

val orders = RestAssuredClient.builder().port(8081).build()
Given(orders)(_.queryParam("status", "open"))
.When(_.get("/orders"))
.Then(_.statusCode(200))

There are examples in RestAssuredScalaExtensionsTest.

The scala-support module adds an alias to the “then” method defined in the Response or MockMvcResponse called “Then”. The reason for this is that then might be a reserved keyword in Scala in the future and the compiler issues a warning when using a method with this name. To enable the use of Then simply import the io.restassured.module.scala.RestAssuredSupport.AddThenToResponse class from the scala-support module. For example:

import io.restassured.RestAssured.when
import io.restassured.module.scala.RestAssuredSupport.AddThenToResponse
import org.hamcrest.Matchers.equalTo
import org.junit.Test
@Test
def `trying out rest assured in scala with implicit conversion`() {
when().
get("/greetJSON").
Then().
statusCode(200).
body("key", equalTo("value"))
}

Note that this is also supported for the Spring Mock Mvc Module.

To use it do like this:

libraryDependencies += "io.rest-assured" % "scala-support" % "7.0.0-RC1"
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>scala-support</artifactId>
<version>7.0.0-RC1</version>
<scope>test</scope>
</dependency>
testImplementation 'io.rest-assured:scala-support:7.0.0-RC1'