Synchronous Testing In Akka ToolKit | Testing Classic Akka Actors

Reading Time: 3 minutes

Akka, a free open source toolkit simplifying the construction of concurrent and distributed systems/applications. In this blog, we are gonna discuss Testing the Akka Actors Synchronously.

Usually, we people say that testing the Akka Actors is a bit confusing and tricky too, but it isn’t. Coming to the types of testing in Akka Toolkit, we have two types of testing i.e. Synchronous Testing and Asynchronous testing. It’s just like we have Unit and Integration testing in Scala.

  1. Synchronous testing –> These are unit testing where we don’t need any ActorSystem, we can directly test the receive method.
  2. Asynchronous Testing –> These are Integration testing, where we can multiple actors working together. Thus, in order to do so, we need to inherit TestKit, instantiating TestProbes.

Synchronous Testing –

Testing the business logic inside Actor classes can be divided into two parts: first, each atomic operation must work in isolation, then sequences of incoming events must be processed correctly, even in the presence of some possible variability in the ordering of events. The former is the primary use case for single-threaded unit testing, while the latter can only be verified in integration tests.

Normally, the ActorRef shields the underlying Actor instance from the outside, the only communications channel is the actor’s mailbox. This restriction is an impediment to unit testing, which led to the concept of the TestActorRef

In this Blog we’ll using ActorSystem, to discuss some another concepts too.

1. Adding Dependencies-

Akka Toolkit comes with a dedicated akka-testkit to test the Akka Actors. To test the akka actors, add the following dependencies to the build.sbt file.

This Dependency will help you in accessing the testkit to test Akka Actors.

2. Create Actor –

Let’s quickly create an Actor which later on will be tested using the synchronous testing.

Since, we have created an Actor Counter which includes basic three operations i.e. Read, Increase and Decrease a value. The value initially is set to Zero (0).

3. Test the Actor –

Let’s test the actor using WordSpecLike and test these operations.

Although it might possible, if you have doubt that why we had used ActorSystem as implicits. The reason being is while testing we need implicit ActorSystem.

Alternatively we can do the same as follows.

Conclusion –

In This Blog we have discussed about the Synchronous Testing in Scala with an example elaborated in each steps.

Written by 

Kuldeepak Gupta is a passionate software consultant at Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. His current passions include utilizing the power of Scala, Akka, and Play to make Reactive and Big Data systems. He is a self-motivated, enthusiastic person who is recognized as a good team player, dedicated, responsible professional, and a technology enthusiast. His hobbies include playing hockey, participating in Political debates, Reading Tech blogs, and listening to songs.

Leave a Reply