scala
import akka.actor.{Actor, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
class MyActor extends Actor {
def receive: Receive = {
case "hello" => sender() ! "world"
}
}
class MyActorTest extends TestKit(ActorSystem("MyActorTest"))
with ImplicitSender
with WordSpecLike
with Matchers
with BeforeAndAfterAll {
override def afterAll: Unit = {
TestKit.shutdownActorSystem(system)
}
"MyActor" should {
"respond with 'world' when receiving 'hello'" in {
val myActor = system.actorOf(Props[MyActor])
myActor ! "hello"
expectMsg("world")
}
}
"MyActor" should {
"respond with 'world' when receiving 'hello'" in {
val probe = TestProbe()
val myActor = system.actorOf(Props[MyActor])
myActor.tell("hello", probe.ref)
probe.expectMsg("world")
}
}
}