Dependencies and setup

To automate repetitive tasks such as setting up testing environments for actors, Akka Typed provides a test kit in the same way Akka untyped does. We need the following dependencies to be present in build.sbt so that we can use it within our project:

"com.typesafe.akka" %% "akka-actor-testkit-typed" % akkaVersion % Test,
"org.scalatest" %% "scalatest" % "3.0.5" % Test

Having both of them in scope will allow us to create ScalaTest specifications and use the Akka Typed test kit functionality.

As mentioned previously, in regards to synchronous actor testing, we do not need to have an ActorSystem. The only dependency, in this case, is an actor context. Akka provides a factory for building special testing actor contexts in the form of the BehaviorTestKit. A skeleton of the ScalaTest specification, in this case, could look as follows:

import akka.actor.testkit.typed.scaladsl.BehaviorTestKit
import org.scalatest.WordSpec

class SynchronousSpec extends WordSpec {

"TestScenario" should {
"have test conditions" in {
val testKit = BehaviorTestKit(behaviorToTest)
// ... testing logic
}
}
}

In the case of asynchronous testing, we have to extend the ActorTestKit to have a test actor system in the scope of the specification. This actor system needs to be shut down after all tests are finished running in order to prevent resource leakage. Because of this, the minimal specification in the case of asynchronous testing will look a bit more involved:

class AsyncronousSpec extends WordSpec with ActorTestKit with BeforeAndAfterAll {

override def afterAll: Unit = shutdownTestKit()

// actual testing code
}

Now, it is time to look at the different features Akka TestKit has to offer to simplify checking the correctness of the actor-based system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.19.174