<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.12</artifactId>
<version>2.6.12</version>
<scope>test</scope>
</dependency>
import akka.actor.AbstractActor;
public class SquarerActor extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(Integer.class, number -> {
int square = number * number;
sender().tell(square, self());
})
.build();
}
}
import akka.testkit.javadsl.TestKit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SquarerActorTest extends TestKit {
private ActorSystem system;
public SquarerActorTest() {
super(ActorSystem.create());
system = ActorSystem.create();
}
@Before
public void setUp() {
system.actorOf(Props.create(SquarerActor.class), "squarerActor");
}
@After
public void tearDown() {
TestKit.shutdownActorSystem(system);
system = null;
}
@Test
public void testSquare() {
int number = 5;
int expectedSquare = 25;
new TestKit(system) {{
getSystem().actorSelection("/user/squarerActor")
.tell(number, getRef());
expectMsg(expectedSquare);
}};
}
}