Testing participants using dependency injection in the 2.4.x game environment

How to test an actor created by dependency injection? In my application, I can get an ActorRef by the name of the injection:

public MyClass { @Inject @Named("ping") ActorRef mPingRef; } 

How do I get this link in my tests?

This is my actor:

 public class PingActor extends UntypedActor { @Inject public PingActor(Configuration configuration) { ... // Use config } @Override public void onReceive(Object message) throws Exception { if (message instanceof Ping) { getSender().tell(new Pong(), getSelf()); } } public static class Ping {} public static class Pong {} } 

I configured my application using my own module:

 public class MyModule extends AbstractModule implements AkkaGuiceSupport { private final Configuration mConfig; public MyModule(Environment environment, Configuration configuration){ this.mConfig = configuration; } @Override protected void configure() { bindActor(PingActor.class, "ping"); } } 

The module is included in application.conf:

 play.modules.enabled += "com.my.package.MyModule" 
+6
source share
2 answers

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() } 
+4
source

I write acting unit tests as such

 static ActorSystem system; static Configuration configuration; static MyActor myActor; @BeforeClass public static void setup() { Map<String, Object> stringConf = new HashMap<>(); configuration = new Configuration(stringConf); system = ActorSystem.apply(); final Props props = Props.create(MyActor.class, configuration); final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "myActor"); myActor = ref.underlyingActor(); } @AfterClass public static void teardown() { JavaTestKit.shutdownActorSystem(system); system = null; } 

then you can call methods on your actor, as if it were a regular Java class. According to the playback structure https://www.playframework.com/documentation/2.5.x/JavaFunctionalTest

As a rule, it is recommended that you enter members only in functional tests and manually create instances in unit tests.

so this is what i am doing here. You will need a dependency on

 "com.typesafe.akka" % "akka-testkit_2.11" % "2.4.12" % "test" 

for this. Hope this helps.

+2
source

All Articles