title: Akka TestProbe to test context.watch() / Terminated handling
body: <p>I'm testing an akka system using TestKit . One actor of the system I'm testing, upon receiving a certain message type, <code>context.watch</code>es the sender, and kills itself when the sender dies:</p>
  
  <pre><code>trait Handler extends Actor {
      override def receive: Receive = {
          case Init =&gt; context.watch(sender)
          case Terminated =&gt; context.stop(self)
      }
  }
  </code></pre>
  
  <p>In my test I'm sending </p>
  
  <pre><code>val probe = TestProbe(system)
  val target = TestActorRef(Props(classOf[Handler]))
  probe.send(target, Init)
  </code></pre>
  
  <p>Now, to test the watch / Terminated behavior - I want to simulate the testprobe being killed.</p>
  
  <p>I can do</p>
  
  <pre><code>probe.send(target, Terminated)
  </code></pre>
  
  <p>But, this presupposes that target has called <code>context.watch(sender)</code> , else it would not receive a Terminated.</p>
  
  <p>I can do</p>
  
  <pre><code>probe.testActor ! Kill
  </code></pre>
  
  <p>with doesn't send <code>Terminated</code> unless target has correctly called <code>context.watch(sender)</code> , but I don't actually want the testprobe killed, as it needs to remain responsive to test if (for example) target continues to send messages instead of stopping itself .</p>
  
  <p>I'm come across this a few times now, what's the correct way to test if an actor is handling the above situation correctly?</p>