Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a TestActorRef in Scala for an Actor with constructor params?

Tags:

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.

like image 920
David Avatar asked Jun 05 '13 20:06

David


1 Answers

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.

like image 74
senia Avatar answered Nov 07 '22 18:11

senia