This page discusses how I can use Guice in my test code:
It shows how to get an instance of a component using injector.instanceOf. What I could not find though is information on injecting Akka actors.
How can I inject an instance of an Akka actor?
Thanks
Rather than injecting actor instances, I’m now creating my actors using system.actorOf and passing the required parameters.
I figured out how to do it by looking at the Play code.
First you define your actor in a Guice module:
package modules
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
class AkkaModule extends AbstractModule with AkkaGuiceSupport {
def configure() = {
bindActor[MyAkkaActor]("myAkkaActor")
}
}
And add this module to your application.conf:
...
play.modules.enabled += "modules.AkkaModule"
...
Then in your test suite you declare a case class that has an actor injected into it:
import javax.inject.{ Inject, Named }
case class MyNamedAkkaActor @Inject() (@Named("myAkkaActor") actor: ActorRef)
Then inject the ActorRef into a variable:
import play.api.inject.guice.GuiceApplicationBuilder
val app = new GuiceApplicationBuilder().build
val myActor = app.injector.instanceOf[MyNamedAkkaActor].actor
Then you can send messages to the actor myActor in your test cases.