gpt4 book ai didi

playframework - 在 play framework 2.4.x 中使用依赖注入(inject)测试 actor

转载 作者:行者123 更新时间:2023-12-03 16:21:07 24 4
gpt4 key购买 nike

如何测试由依赖注入(inject)创建的参与者?在我的应用程序中,我可以通过命名注入(inject)获得 ActorRef:

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

如何在我的测试中获得此引用?

这是我的 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 {}
}

我已经用我自己的模块配置了我的应用程序:
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");
}
}

该模块在 application.conf 中启用:
play.modules.enabled += "com.my.package.MyModule"

最佳答案

此解决方案适用于 PlayScala ,但它应该与您的 PlayJava 的机制相同:

所以我得到了我的GuiceModule :

class CommonModule extends AbstractModule with AkkaGuiceSupport {
override def configure(): Unit = {
bindActor[SomeActor]("actor-name")
}
}

然后是测试(我从测试中剥离了一些东西,所以它可能无法直接编译):
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")
}
}
}
}

所以我所做的是:
  • 创造新鲜ActorSystem
  • 把它包起来actorSystem在 Akka 的 TestKit ( libraryDependencies += "com.typesafe.akka" %% "akka-testkit" % "2.4.1" )
  • 使用 GuiceApplicationBuilder应用覆盖
  • 然后使用 app.injector直接访问我的 guice 配置的 Actor

  • 当您查看 bindActor 的实现时,很明显会发生什么。您在 MyModule.configure() 中使用的方法:
      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()
    }

    关于playframework - 在 play framework 2.4.x 中使用依赖注入(inject)测试 actor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32669886/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com