Inspect Actor Internal State in Unit Test

You aren’t supposed to access an actor except through the ActorRef, but if you really need to (e.g. for a unit test), it is possible. actorOf can take a block, in which you can do anything so long as you return an Actor. So, you can directly access the Actor like this:

import akka.actor._

class MyActor extends Actor {
  var data = 0
  def receive = {
    case i: Int => data += i
    case 'show => println(data)
  }
}

var actor: MyActor = _
val actorRef = Actor.actorOf {
  actor = new MyActor
  actor
}
actorRef.start()
actor.data // => 0
actorRef ! 1
actor.data // => 1
actorRef ! 10
actor.data // => 11