This is a solution for PlayScala , but it should be the same mechanism for your PlayJava :
So I got GuiceModule :
class CommonModule extends AbstractModule with AkkaGuiceSupport { override def configure(): Unit = { bindActor[SomeActor]("actor-name") } }
Then the test (I removed some things from my test, so it cannot compile directly):
import akka.actor.{ActorRef, ActorSystem} import akka.testkit.{TestKit, TestProbe} import module.CommonModule import org.specs2.mutable.Specification import org.specs2.specification.Scope import play.api.inject._ import play.api.inject.guice.GuiceApplicationBuilder import play.api.test.Helpers._ class SwitchUpdateActorSpec extends Specification { "MyActor" should { val actorSystem = ActorSystem("test") class Actors extends TestKit(actorSystem) with Scope val app = new GuiceApplicationBuilder(modules = Seq(new CommonModule)) .overrides(bind[ActorSystem].toInstance(actorSystem)) .build() "respond with 'ok' upon receiving a message" in new Actors { running(app) { private val injector: Injector = app.injector private val actor: ActorRef = injector.instanceOf(BindingKey(classOf[ActorRef]).qualifiedWith("actor-name")) val probe = TestProbe() actor.tell("hi there!", probe.ref) probe.expectMsg("ok") } } } }
So what I did:
- create a new
ActorSystem - wrap the
ActorSystem in Akka TestKit ( libraryDependencies += "com.typesafe.akka" %% "akka-testkit" % "2.4.1" ) - use
GuiceApplicationBuilder to apply override - and then use
app.injector directly to access my customized schedule.
Obviously, what happens when you look at the bindActor implementation that you use in your MyModule.configure() method:
def bindActor[T <: Actor: ClassTag](name: String, props: Props => Props = identity): Unit = { accessBinder.bind(classOf[ActorRef]) .annotatedWith(Names.named(name)) .toProvider(Providers.guicify(Akka.providerOf[T](name, props))) .asEagerSingleton() }
source share