liveBook · Manning

TestProbe

As explained in Akka in Action, Second Edition

TestProbe is an Akka Typed test utility that receives messages from actors and provides operations for inspecting and asserting them.

TestProbe is an asynchronous Akka Typed testing utility that receives an actor’s messages so tests can assert externally visible behavior. It runs with ActorTestKit in a real ActorSystem, where actors may execute in parallel and message arrival order is not guaranteed. This makes it useful when stubbed synchronous tests could miss scheduling or asynchronous behavior. section 4.1 section 4.3 section 4.4

Assert a forwarded message

Spawn the actor, create a typed probe, pass probe.ref as the reply destination, and assert the received value. expectMessage consumes the matched message from the probe inbox. Keep one ActorTestKit for the suite and shut it down afterward, since creating ActorSystems repeatedly is costly. section 4.3.1 section 4.2.2

Listing 4.2 Essential parts of an async test
import akka.actor.testkit.typed.scaladsl.ActorTestKit
import org.scalatest.BeforeAndAfterAll
 
class AsyncForwardSpec
    extends AnyWordSpec
    with BeforeAndAfterAll
    with Matchers {
 
  val testKit = ActorTestKit()                                       #1
 
  "a Simplified Manager" must {
 
    "actor gets forwarded message from manager" in {
      val manager = testKit.spawn(SimplifiedManager())               #2
      val probe = testKit.createTestProbe[String]()                  #3
      manager ! SimplifiedManager.Forward("message-to-parse", probe.ref) 
      probe.expectMessage("message-to-parse")                        #4
    }
  }
 
  override def afterAll(): Unit = testKit.shutdownTestKit()          #5
}

A minimal asynchronous test shows the complete lifecycle: create ActorTestKit, spawn the manager, create TestProbe[String], send Forward, expect the forwarded message, and terminate the test kit. section 4.3.1

A probe can also monitor an actor rather than merely receive a reply. Wrap a behavior with Behaviors.monitor(probe.ref, behavior), then send messages to the spawned monitored actor. Each message sent to that actor is also delivered to the probe, allowing tests to inspect a message cascade without changing the actor protocol. section 4.3.1 section 4.4

For nondeterministic message streams, fishForMessage requires an explicit outcome for every message encountered:

  • Complete while keeping the expected message. section 4.3.2
  • Continue while keeping or discarding an irrelevant message. section 4.3.2
  • Fail immediately, optionally disregarding earlier catches. section 4.3.2

Wait for a timer-driven resume

To test a counter that resumes after Pause, monitor the counter with a probe and fish within a timeout. Ignore Increase and Pause messages, but complete when Resume arrives. Set the fishing timeout longer than the requested pause so the test has time to observe the scheduled effect. section 4.3.2

TestProbe is the asynchronous counterpart to TestInbox: both let tests inspect messages, but TestProbe has a richer API and works with a complete ActorSystem. ScalaTestWithActorTestKit can manage the test kit lifecycle and makes spawn and createTestProbe directly available in a test. section 4.3 section 4.3.2

How Akka in Action, Second Edition covers it

How deep it goes
Demonstrates message expectations, actor monitoring, time-bounded assertions, and fishing outcomes; cluster testing remains outside scope.
Examples
Uses a forwarding manager, a monitored behavior, and a pausing counter that emits Resume.
What it assumes
Assumes familiarity with actor protocols and ActorRef message sending while introducing asynchronous test setup.
What you can do after
Apply it via ActorTestKit to assert replies, monitor message flows, and await selected asynchronous messages.
How it connects
Connects ActorTestKit, TestInbox, BehaviorTestKit, Behaviors.monitor, ActorRef, and ScalaTestWithActorTestKit.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.