liveBook · Manning

BehaviorTestKit

As explained in Akka in Action, Second Edition

BehaviorTestKit is an Akka Typed testing utility that runs actor behavior under test and exposes generated effects, messages, and log output for inspection.

BehaviorTestKit is a synchronous Akka Typed test utility for exercising one actor behavior in isolation. Create it from an initial Behavior, send a protocol message with run, and inspect the recorded consequences instead of accessing actor state directly. It is suited to deterministic unit tests of actor-context actions such as spawning, messaging, and logging. section 4.1 section 4.2.1

The kit runs against a stubbed actor context rather than a real ActorSystem. Context actions become Effect values stored in a queue, so assertions can poll expected effects in a known order. This makes tests repeatable, while deliberately exposing implementation-level actions as a white-box testing style. section 4.2.1 section 4.4

Assert that a manager spawns a worker

Start with BehaviorTestKit(SimplifiedManager()). Assert NoEffects before any input. Run CreateChild("adan"), then expect Spawned(SimplifiedWorker(), "adan"). The test checks both the absence of premature work and the exact spawn caused by the command. section 4.2.1

Logging is captured through the same queue-based approach. After running a command that calls context.log.info, inspect logEntries and compare the resulting CapturedLogEvent by level and message. Captured events avoid coupling the assertion to a particular logging implementation. section 4.2.3

BehaviorTestKit does not model scheduled messages, asynchronous calls, or multi-actor interaction. Use ActorTestKit when a real ActorSystem, parallel execution, nondeterministic message order, probes, or scheduled behavior must be tested. section 4.2.3 section 4.4

Listing 4.1 Simplified manager testing spawning
import akka.actor.testkit.typed.scaladsl.BehaviorTestKit
import akka.actor.testkit.typed.Effect.{ NoEffects, Scheduled, Spawned }
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
 
class SyncTestingSpec extends AnyWordSpec with Matchers {
 
  "Typed actor synchronous testing" must {
 
    "spawning takes place" in {
      val testKit = BehaviorTestKit(SimplifiedManager())
      testKit.expectEffect(NoEffects)                             #1
      testKit.run(SimplifiedManager.CreateChild("adan"))          #2
      testKit.expectEffect(Spawned(SimplifiedWorker(), "adan"))   #3
    }
  }
}

This compact test shows the full synchronous cycle: initialize the kit, verify no initial effects, run a command, and assert the spawned child effect. section 4.2.1

How Akka in Action, Second Edition covers it

How deep it goes
Explains deterministic queue-backed effect and log assertions, while deferring scheduling, asynchronous calls, and multi-actor testing.
Examples
Uses a manager that spawns an ignoring worker and a manager command that emits an INFO log event.
What it assumes
Assumes familiarity with Akka Typed behaviors, actor messages, and actor-context actions such as spawn and logging.
What you can do after
Apply it via BehaviorTestKit to deterministically assert an isolated actor’s spawned effects and captured log output.
How it connects
Connects BehaviorTestKit with ActorSystem, ActorRef, TestInbox, ActorTestKit, TestProbe, Effects, and logging.

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.