Akka Actor Test: Automatic reply with a TestProbe
<p>I am trying to get a test probe to reply with an acknowledgement, whenever it receive any message . </p>

<p>I wrote the following code in my test but it does not work: </p>

<pre><code>val chgtWriter = new TestProbe(system)  {

          def receive: Receive = {

            case m =&gt; println("receive messagereplying with ACK"); sender() ! ACK

          }

        }
</code></pre>

<p>Is there a way to do that. The actor that is actually sending the message to the test probe is definitely running on another thread than the TestThread. Below you can see the full test as currently crafted.</p>

<pre><code>feature("The changeSetActor periodically fetch new change set following a schedule") {


scenario("A ChangeSetActor fetch new changeset from a Fetcher Actor that return a full and an empty ChangeSet"){


  Given("a ChangeSetActor with a schedule of fetching a message every 10 seconds, a ChangeFetcher and a ChangeWriter")

    val chgtFetcher = TestProbe()

    val chgtWriter = new TestProbe(system)  {

      def receive: Receive = {

        case m =&gt; println("receive message {} replying with ACK"); sender() ! ACK

      }

    }
    val fromTime = Instant.now().truncatedTo(ChronoUnit.SECONDS)
    val chgtActor = system.actorOf(ChangeSetActor.props(chgtWriter.ref, chgtFetcher.ref, fromTime))

  When("all are started")


  Then("The Change Fetcher should receive at least 3 messages from the ChangeSetActor within 40 seconds")

    var changesetSNum = 1

    val received = chgtFetcher.receiveWhile( 40 seconds) {

      case FetchNewChangeSet(m) =&gt; {

        println(s"received: FetchNewChangeSet(${m}")

        if (changesetSNum == 1) {
            chgtFetcher.reply(NewChangeSet(changeSet1))
            changesetSNum += 1
          }
          else
            chgtFetcher.reply(NoAvailableChangeSet)
        }

      }

    received.size should be (3)

}
</code></pre>

<p>}</p>

<p>The changeSetActor is fully tested and works. The test hang with the ChangeWriter. It never receive a message in the receive method.</p>

<p><strong>EDIT1(Following @Jakko anser)</strong></p>

<p>The <a href="http://doc.akka.io/docs/akka/current/scala/testing.html#Auto-Pilot" rel="noreferrer">Auto Pilots</a> is as follow: </p>

<blockquote>
<pre><code>val probe = TestProbe()
probe.setAutoPilot(new TestActor.AutoPilot {
  def run(sender: ActorRef, msg: Any): TestActor.AutoPilot =
    msg match {
      case "stop" ⇒ TestActor.NoAutoPilot
      case x      ⇒ **testActor.tell(x, sender)**; TestActor.KeepRunning
    }
})
</code></pre>
</blockquote>

<p>Although all the explanation given so far is way clear, what is confusing in the official example is the reference "testActor". Who is testActor here ? there is no variable declaration of that name at that point.</p>