DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems
  • More Tests, More Confidence? Test Suites Are Investment Portfolios
  • One of Waterfall's Most Resilient Artifacts

Trending

  • Build a Voice Assistant With Gemini Live API
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Avoiding AI Debt as a Team
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Mocking Methods for Testing Akka HTTP Routes

Mocking Methods for Testing Akka HTTP Routes

Here's an intro to using Mockito to mock your methods when it comes to unit testing Akka HTTP Routes.

By 
Manjot Kaur
·
Sep. 03, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this blog, I am going to explain how to write unit test cases for 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 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:

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:

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"
      }
    }


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:

  • http://www.scalatest.org/user_guide/testing_with_mock_objects
  • http://blog.madhukaraphatak.com/akka-http-testing/
  • https://doc.akka.io/docs/akka-http/current/routing-dsl/testkit.html

This article was first published on the Knoldus blog.

unit test Akka (toolkit)

Published at DZone with permission of Manjot Kaur. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems
  • More Tests, More Confidence? Test Suites Are Investment Portfolios
  • One of Waterfall's Most Resilient Artifacts

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook