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
