Testing Actors While Making Multiple calls to Other actors without replying Directly to sender

(Srihari Kusumanchi) #1

I have three actors in My Project Lets Consider A,B,C

A will send message to B actor and B actor will do some action and creates ActorRef for C actor and then C actor will send a message to Sender actor in our scenario which is B actor and once B actor receives the response from C actor It’ll do some action and sends final response To A actor.

In Actor A :
b ! messageFromA(aActorRef)

In Actor B:
case messageFromA(x:ActorRef)=>
val x=methodInB1()
cActorRef ! x
case messageFromC(responseFromC)=>

In Actor C:
case x=>sender ! messageFromC(responseFromC)

In the above code, you can clearly see that the Actor B is following some flow and not replying directly to senders. I need how to write the unit test case for this Actor B with full coverage. How to Implement can someone please suggest.
For Both the messages in Actor B it’s not replying to any message and Instead, it perform some action and calling other actors,so we cannot expect the direct reply for any message while writing the test case .

(Johan Andrén) #2

The most common way would be to make sure you can use a test probes in place of constructed children and other actors, for example a def createChildB() will allow you to subclass the actor in a test and return the ActorRef of a probe. For actors that aren’t children you can either inject the needed actorref which makes things very easy, or make sure the lookup logic is in a method you can override.

This will let you test the respective actors in isolation instead of the whole flow.

The testing section of the docs has some samples that may be useful (the “Externalize child making from the parent” for example): https://doc.akka.io/docs/akka/current/testing.html