Search code examples
scalatddakkascalatest

Test actors creation in akka hooks (preStart)


I want to test that preStart() creates right actors tree (Correct me, if I choose wrong place to create actors tree).

class Central extends Actor { 

  var summer : ActorRef = _

  override def preStart() = {
    val printerProps = Props[Printer]
    val printer = context.actorOf(printerProps, "printer")
    val summerProps = Props(new Summer(printer))
    summer = context.actorOf(summerProps, "summer")
  }

  override def receive = {
    case msg =>
  }
}

For full picture:

class Printer extends Actor {
  override def receive = {
    case msg => println(msg)
  }
}

class Summer(printer: ActorRef) extends Actor {
  override def receive = {
    case (x: Int, y: Int) =>
      printer ! x + y
  }
}

Any idea, how to make clear test of it?

This answer https://stackoverflow.com/a/18877040/1768378 is close for what I'am looking for. But I think that change code because a test reason is bad idea.

Maybe someone knows the better solution.


Solution

  • IF you want to test just creation, you can also, from your test, get from the ActorSystem an ActorSelection containing all the children of your Central actor (central/*).

    After that, send an Identify message (special akka message) to the whole selection and wait for the responses, checking whether they match. No need to inject code in your actors.