I have ParentActor and ChildActor as follows:
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. Can you help me with this?
Change your parent - inject Child
actor Props
via constructor. And change your Child
- inject parent reference via constructor. In test when creating Parent
inject another Child
Props
which accepts test actor as a parameter. 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. And you can check response via expectMsgEquals
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.