Search⌘ K
AI Features

Testing the Routes: Use Case - Saving

Explore how to test HTTP routes in Scala focusing on product saving scenarios. Understand handling invalid garbage data with proper error responses, managing attempts to save duplicate products, and successfully saving valid products. This lesson teaches you how to verify API responses and database integrity through unit and integration tests.

Garbage value

We’ll continue with the use case of saving (or creating) a product via the API. We are going to generate junk data to test our APIs.

Scala
val expectedStatus = StatusCodes.BadRequest
s"return $expectedStatus" in {
for {
resp <- http.singleRequest(
HttpRequest(
method = HttpMethods.POST,
uri = s"$baseUrl/products",
headers = Seq(),
entity = HttpEntity(
contentType = ContentTypes.`application/json`,
data = ByteString(
scala.util.Random.alphanumeric.take(256).mkString
)
)
)
)
} yield {
resp.status must be(expectedStatus)
}
}

Here, we test posting ...