Mocking Methods for Testing Akka Http Routes

Table of contents
Reading Time: 2 minutes

Hey folks, in this blog I am going to explain how to write unit test cases for the routes in Akka Http.

First and foremost the test cases should not hit our backend logic, remember that we are only testing our routes, routes basically form the controller layer in our application. They control the request/response cycle. They tell that which business logic should respond to the request and send the control to the corresponding business layer logic. We must always follow best coding practices while defining our routes.

So let us understand how to write unit test cases for the routes with the help of an example:

Suppose that we have a simple route which handles a “/adduser” post request :



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


trait RestService {
implicit val userFormat = jsonFormat2(User)
val userImpl: UserImpl
val route =
post {
path("adduser") {
entity(as[User]) { user =>
val saved: Future[Done] = userImpl.addUser(user)
onComplete(saved) { _ =>
complete("user added")
}
}
}
}
}
class RestServiceImpl extends RestService {
val userImpl = UserImpl
}

Now, in this route user.Impl.add(user) is calling the backend business logic for adding a user to the database, so this method needs to be mocked while testing otherwise this method would be called every time we test this route,  which is not an ideal scenario for unit testing.

There are many ways to mock this method, I have used Mockito, a mocking framework for unit testing, to mock this method while testing my post route.

Using Mockito is very easy, we just need to import the following library dependency and then write our unit test.

libraryDependencies += org.mockito % mockito-all % 1.9.5 % Test

Now, the unit test case for this post route can be written like this :



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


class RestSpec extends WordSpec with Matchers with ScalatestRouteTest with MockitoSugar {
val mockUserImpl = mock[UserImpl]
object TestObject extends RestService {
val userImpl = mockUserImpl
}
"The service" should {
"return user added as response for a Post request to /adduser" in {
when(mockUserImpl.addUser(User(2, "test"))).thenReturn(Future.successful(Done))
val jsonRequest = ByteString(
s"""
|{
| "id":2,
| "name":"test"
|}
""".stripMargin)
val postRequest = HttpRequest(
HttpMethods.POST,
uri = "/adduser",
entity = HttpEntity(MediaTypes.`application/json`, jsonRequest))
postRequest ~> Route.seal(TestObject.route) ~> check {
status.isSuccess() shouldEqual true
responseAs[String] shouldEqual "user added"
}
}
view raw

RestSpec.scala

hosted with ❤ by GitHub

Firstly, we need to extend ScalaTest’s MockitoSugar trait that provides some basic syntax sugar for Mockito.

Then, in this test case, I have mocked the UserImpl object using the Mockito framework. and then the mocked instance is used with the when/then pattern to mock the adduser method.

I am sending the user information in json format in the body of post request and finally checking the assertions.

References :

blog-footer

Written by 

Manjot Kaur is a software consultant, having more than 0.5 years of experience. She likes to explore new technologies and trends in the IT world. Her hobbies include Travelling, watching movies and running. She is currently working on Technologies like scala with maven, dynamoDb, Akka-Http.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading