import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.testkit.javadsl.TestKit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class MyActorTest {
static ActorSystem system;
@BeforeClass
public static void setup() {
system = ActorSystem.create();
}
@AfterClass
public static void teardown() {
TestKit.shutdownActorSystem(system);
system = null;
}
@Test
public void testMyActor() {
new TestKit(system) {{
final ActorRef myActor = system.actorOf(MyActor.props(), "myActor");
myActor.tell("Hello", getRef());
expectMsg("World");
}};
}
static class MyActor extends AbstractActor {
static Props props() {
return Props.create(MyActor.class, MyActor::new);
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(String.class, msg -> {
if (msg.equals("Hello")) {
getSender().tell("World", getSelf());
} else {
unhandled(msg);
}
})
.build();
}
}
}