title: How to mock child Actors for testing an Akka system?
body: <p>When I have a parent actor in Akka, that directly creates a child actor on initialisation, when I want to write unit tests for the parent actor, how can I replace the child actor with a TestProbe or a mock?</p>
  
  <p>For example, with the following contrived code sample: </p>
  
  <pre><code>class TopActor extends Actor {
    val anotherActor = context.actorOf(AnotherActor.props, "anotherActor")
  
    override def receive: Receive = {
      case "call another actor" =&gt; anotherActor ! "hello"
    }
  }
  
  class AnotherActor extends Actor {
  
    override def recieve: Receive = {
      case "hello" =&gt; // do some stuff
    }
  
  }
  </code></pre>
  
  <p>If I want to write a test for TopActor, to check the message sent to AnotherActor is "hello", how do I replace the implementation of AnotherActor? It seems like TopActor creates this child directly so this is not easy to access.</p>