Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka. How to mock child actors in java?

Tags:

java

akka

Let's say I have a parent actor who creates Actor by himself.

public static class Parent extends UntypedActor {

private ActorRef child = context().actorOf(Props.create(Child.class));

    @Override
    public void onReceive(Object message) throws Exception {
        // do some stuff
        child.tell("some stuff", self());
    }
}

public static class Child extends UntypedActor {

    @Override
    public void onReceive(Object message) throws Exception {

    }
}

How can I mock this child Actor? Google didn't give me any reasonable results. I've been told by Akka's documentation that creating an actor is a good practice. But how can I follow this practice if I can't even test my actors?

like image 491
Artem Malinko Avatar asked Nov 09 '22 18:11

Artem Malinko


1 Answers

I use probes as described in this answer to a similar question:

How to mock child Actors for testing an Akka system?

ActorSystem system = ActorSystem.create();

new JavaTestKit( system )
{{
  final JavaTestKit probe = new JavaTestKit( system );
  final Props props = Props.create( SupervisorActor.class );

  final TestActorRef<SupervisorActor> supervisorActor =
    TestActorRef.create( system, props, "Superman" );

  supervisorActor.tell( callCommand, getTestActor() );

  probe.expectMsgEquals(42);
  assertEquals(getRef(), probe.getLastSender());
}};
like image 168
AndyKenZ Avatar answered Nov 14 '22 21:11

AndyKenZ