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.
- Synchronous testing –> These are unit testing where we don’t need any ActorSystem, we can directly test the receive method.
- 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.
3. Test the Actor –
Let’s test the actor using WordSpecLike and test these operations.
Alternatively we can do the same as follows.
In This Blog we have discussed about the Synchronous Testing in Scala with an example elaborated in each steps.