Writing unit test for actors
When we develop an Akka application, we should write an automated test for our actors to check whether they are working as expected. Since the actor model is different as they are asynchronous in nature, we have a different view of how we will perform unit tests on them.
Getting ready
First of all, we will add the akka-testkit
dependency to our application's build.sbt
. So, add the following dependency to build.sbt
:
libraryDependencies += "com.typesafe.akka" % "akka-testkit_2.11" % "2.4.4" libraryDependencies += "org.scalatest" % "scalatest_2.11" % "3.0.1"
Now move to the project root directory and run the sbt update
command; it will download the preceding dependencies. Now import the Hello-Akka
project in the IDE.
How to do it…
- Create a
com.packt.chapter5
package in thesrc/test/scala
project folder. - Create a Scala file, say,
Testing.scala
, in thecom.packt.chapter5
package. - Add the following import to the top of the file:
import akka.actor.{Actor, ActorSystem...