[英]How to test that akka actor message was sent to it's child
I have ParentActor and ChildActor as follows: 我有如下的ParentActor和ChildActor:
class ParentActor extends UntypedActor {
final ActorSystem system = ActorSystem.create("MySystem");
final ActorRef child = system.actorOf(Props.create(ChildActor.class), "child")
@Override
void onReceive(Object message) throws Exception {
if (message instanceof String) {
getContext().watch(child)
child.tell(message, getSelf())
} else {
unhandled(message)
}
}
}
class ChildActor extends UntypedActor {
@Override
void onReceive(Object message) throws Exception {
if (message instanceof String) {
context().parent().tell("Hello from child actor!", self());
}
}
}
I want to test the relationship between parent and child (If I send String message to ParentActor assure that child has got it) using TestKit and in java. 我想使用TestKit和在Java中测试父子之间的关系(如果我将String消息发送给ParentActor以确保子得到了)。 Can you help me with this? 你能帮我吗?
Change your parent - inject Child
actor Props
via constructor. 更改您的父母-通过构造函数注入Child
演员Props
。 And change your Child
- inject parent reference via constructor. 并更改您的Child
通过构造函数注入父级引用。 In test when creating Parent
inject another Child
Props
which accepts test actor as a parameter. 在创建Parent
测试中,注入另一个接受测试演员作为参数的Child
Props
。 Change: 更改:
context().parent().tell("Hello from child actor!", self());
to 至
parentField.tell("Hello from child actor!", self());
Then in your test use akka test actor instead of Parent
and pass it to Child. 然后在您的测试中,使用akka测试演员而不是Parent
,并将其传递给Child。 And you can check response via expectMsgEquals
method. 您可以通过expectMsgEquals
方法检查响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.