<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.13</artifactId>
<version>2.6.17</version>
</dependency>
import akka.actor.AbstractActor;
public class MyActor extends AbstractActor {
@Override
public Receive createReceive() {
return receiveBuilder()
.match(String.class, message -> {
System.out.println("Received message: " + message);
})
.build();
}
}
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
public class Main {
public static void main(String[] args) {
ActorSystem system = ActorSystem.create("MySystem");
ActorRef myActor = system.actorOf(Props.create(MyActor.class), "myActor");
myActor.tell("Hello, World!", ActorRef.noSender());
system.terminate();
}
}
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.13</artifactId>
<version>2.6.17</version>
<scope>test</scope>
</dependency>
import akka.actor.ActorSystem;
import akka.actor.Props;
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 Props props = Props.create(MyActor.class);
final TestKit probe = new TestKit(system);
final ActorRef myActor = system.actorOf(props, "myActor");
myActor.tell("Hello, TestKit!", probe.getRef());
expectMsgEquals("Received message: Hello, TestKit!");
}};
}
}