Scala, , Java. , TestProbe . :
import akka.actor.{Actor, Props}
class Parent extends Actor {
import Parent._
val child = context.actorOf(Child.props)
override def receive: Receive = {
case TellName(name) => child ! name
case Reply(msg) => sender() ! msg
}
}
object Parent {
case class TellName(name: String)
case class Reply(text: String)
def props = Props(new Parent)
}
class Child extends Actor {
override def receive: Actor.Receive = {
case name: String => sender ! Parent.Reply(s"Hi there $name")
}
}
object Child {
def props = Props(new Child)
}
, Parent, TellName Child. Child , Reply - ", ". :
class ParentSpec extends TestKit(ActorSystem("test")) with WordSpecLike with Matchers with ImplicitSender {
val childProbe = TestProbe()
"Parent actor" should {
"send a message to child actor" in {
childProbe.setAutoPilot(new AutoPilot {
override def run(sender: ActorRef, msg: Any): AutoPilot = msg match {
case name: String => sender ! Reply(s"Hey there $name")
NoAutoPilot
}
})
val parent = system.actorOf(Props(new Parent {
override val child = childProbe.ref
}))
parent ! TellName("Johnny")
childProbe.expectMsg("Johnny") // Message received by a test probe
expectMsg("Hey there Johnny") // Reply message
}
}
}
, Child, setAutoPilot, , . , , Child "", Reply, ", " .
TellName "". , - - childProbe.expectMsg("Johnny"). - expectMsg("Hey there Johnny"). , ", " , ", " , , Child.