Playframework 在play framework 2.4.x中使用依赖项注入测试参与者
如何测试依赖注入创建的参与者?在我的应用程序中,我可以通过命名注入获得ActorRef:Playframework 在play framework 2.4.x中使用依赖项注入测试参与者,playframework,akka,guice,playframework-2.4,Playframework,Akka,Guice,Playframework 2.4,如何测试依赖注入创建的参与者?在我的应用程序中,我可以通过命名注入获得ActorRef: public MyClass { @Inject @Named("ping") ActorRef mPingRef; } 我如何在测试中获得此参考 这是我的演员: public class PingActor extends UntypedActor { @Inject public PingActor(Configuration configuration) {
public MyClass {
@Inject
@Named("ping")
ActorRef mPingRef;
}
我如何在测试中获得此参考
这是我的演员:
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")
}
}
然后是测试(我从测试中剥离了一些内容,因此它可能无法直接编译):
所以我所做的是:
- 创建一个新的
ActorSystem
- 将
包装到Akka的actorSystem
(TestKit
)libraryDependencies+=“com.typesafe.Akka”%%“Akka TestKit”%%“2.4.1”
- 使用
应用覆盖GuiceApplicationBuilder
- 然后直接使用
访问我的GUI配置的actorapp.injector
MyModule.configure()方法中使用的bindActor
的实现时,发生的情况变得非常明显:
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()
}
def bindActor[T Props=identity):单位={
accessBinder.bind(classOf[ActorRef])
.annotatedWith(name.named(name))
.toProvider(Providers.guicify(Akka.providerOf[T](名称、道具)))
.asEagerSingleton()
}
我正在编写参与者单元测试
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;
}
希望这能起作用
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;
}
"com.typesafe.akka" % "akka-testkit_2.11" % "2.4.12" % "test"