The Akka Testing docs give the following way to create a TestActorRef:
import akka.testkit.TestActorRef val actorRef = TestActorRef[MyActor]
How do I extend this for testing an existing actor that takes constructor arguments? When I try running this as is, substituting in my actor class, I get the following error:
"error while creating actor
akka.actor.ActorInitializationException:Could not instantiate Actor
Make sure Actor is NOT defined inside a class/trait,
if so put it outside the class/trait, f.e. in a companion object,
OR try to change: 'actorOf(Props[MyActor]' to 'actorOf(Props(new MyActor)'."
The various ideas I could think up for adding the args after the class name inside the square brackets all crashed and burned, too.
You could use Props
like this:
val actorRef = TestActorRef(Props(new MyActor(param1, param2)))
Or factory method like this:
val actorRef = TestActorRef(new MyActor(param1, param2))
See apply
methods in object TestActorRef
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With