Complete integration testing framework using real ActorSystem for testing actor interactions, message flows, and system behavior. Uses ActorTestKit for system management and TestProbe for message assertion.
Main entry point for asynchronous testing providing actor system management and test utilities.
/**
* Factory methods for creating ActorTestKit instances
*/
object ActorTestKit {
/** Create with default configuration and generated name */
def apply(): ActorTestKit
/** Use existing actor system */
def apply(system: ActorSystem[_]): ActorTestKit
/** Create with custom name */
def apply(name: String): ActorTestKit
/** Create with custom configuration */
def apply(customConfig: Config): ActorTestKit
/** Create with name and custom configuration */
def apply(name: String, customConfig: Config): ActorTestKit
/** Full customization with settings */
def apply(name: String, customConfig: Config, settings: TestKitSettings): ActorTestKit
/** Shutdown actor system */
def shutdown(system: ActorSystem[_]): Unit
/** Shutdown with timeout control */
def shutdown(system: ActorSystem[_], timeout: Duration, throwIfShutdownFails: Boolean): Unit
/** Default test configuration */
val ApplicationTestConfig: Config
}
/**
* TestKit instance for asynchronous testing
*/
class ActorTestKit {
/** The underlying actor system */
implicit def system: ActorSystem[Nothing]
/** Test kit settings */
implicit def testKitSettings: TestKitSettings
/** Default timeout for operations */
implicit val timeout: Timeout
/** System scheduler */
def scheduler: Scheduler
/** Spawn actor with behavior */
def spawn[T](behavior: Behavior[T]): ActorRef[T]
/** Spawn actor with behavior and props */
def spawn[T](behavior: Behavior[T], props: Props): ActorRef[T]
/** Spawn actor with behavior and name */
def spawn[T](behavior: Behavior[T], name: String): ActorRef[T]
/** Spawn actor with behavior, name and props */
def spawn[T](behavior: Behavior[T], name: String, props: Props): ActorRef[T]
/** Stop spawned actor and wait for termination */
def stop[T](ref: ActorRef[T], max: FiniteDuration): Unit
/** Create anonymous test probe */
def createTestProbe[M](): TestProbe[M]
/** Create named test probe */
def createTestProbe[M](name: String): TestProbe[M]
/** Create probe subscribed to unhandled messages */
def createUnhandledMessageProbe(): TestProbe[UnhandledMessage]
/** Create probe subscribed to dead letters */
def createDeadLetterProbe(): TestProbe[DeadLetter]
/** Create probe subscribed to dropped messages */
def createDroppedMessageProbe(): TestProbe[Dropped]
/** Serialization testing utilities */
val serializationTestKit: SerializationTestKit
/** Shutdown the test kit */
def shutdownTestKit(): Unit
}Usage Examples:
import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ActorRef, Behavior}
// Create test kit with default settings
val testKit = ActorTestKit()
// Define a simple behavior for testing
val echoBehavior: Behavior[String] = Behaviors.receiveMessage { msg =>
println(s"Echo: $msg")
Behaviors.same
}
// Spawn actor
val echoActor = testKit.spawn(echoBehavior, "echo-actor")
// Create probe for testing
val probe = testKit.createTestProbe[String]()
// Test interaction
echoActor ! "Hello World"
// Cleanup
testKit.shutdownTestKit()Utilities for creating and managing test actors within the test kit environment.
/**
* Spawn actor with behavior - creates as child of test kit guardian
* @param behavior The behavior to spawn
* @returns ActorRef for the spawned actor
*/
def spawn[T](behavior: Behavior[T]): ActorRef[T]
/**
* Spawn actor with behavior and props
* @param behavior The behavior to spawn
* @param props Actor props for configuration
* @returns ActorRef for the spawned actor
*/
def spawn[T](behavior: Behavior[T], props: Props): ActorRef[T]
/**
* Spawn actor with specific name
* @param behavior The behavior to spawn
* @param name Actor name
* @returns ActorRef for the spawned actor
*/
def spawn[T](behavior: Behavior[T], name: String): ActorRef[T]
/**
* Stop spawned actor and wait for termination
* @param ref ActorRef to stop
* @param max Maximum wait time for termination
*/
def stop[T](ref: ActorRef[T], max: FiniteDuration): UnitFactory methods for creating various types of test probes for message testing and system event monitoring.
/**
* Create anonymous test probe for message testing
* @tparam M Message type the probe accepts
* @returns TestProbe instance
*/
def createTestProbe[M](): TestProbe[M]
/**
* Create named test probe
* @param name Name for the probe
* @tparam M Message type the probe accepts
* @returns TestProbe instance
*/
def createTestProbe[M](name: String): TestProbe[M]
/**
* Create probe subscribed to unhandled messages from event bus
* @returns TestProbe for UnhandledMessage events
*/
def createUnhandledMessageProbe(): TestProbe[UnhandledMessage]
/**
* Create probe subscribed to dead letters from event bus
* @returns TestProbe for DeadLetter events
*/
def createDeadLetterProbe(): TestProbe[DeadLetter]
/**
* Create probe subscribed to dropped messages from event bus
* @returns TestProbe for Dropped events
*/
def createDroppedMessageProbe(): TestProbe[Dropped]Usage Examples:
import akka.actor.testkit.typed.scaladsl.ActorTestKit
import akka.actor.typed.scaladsl.Behaviors
val testKit = ActorTestKit()
// Create various probes
val messageProbe = testKit.createTestProbe[String]()
val namedProbe = testKit.createTestProbe[Int]("counter-probe")
val deadLetterProbe = testKit.createDeadLetterProbe()
// Define behavior that sends to probe
val testBehavior = Behaviors.receiveMessage[String] { msg =>
messageProbe.ref ! s"Processed: $msg"
Behaviors.same
}
val actor = testKit.spawn(testBehavior)
actor ! "test message"
// Verify message received
messageProbe.expectMessage("Processed: test message")
testKit.shutdownTestKit()Methods for configuring the test environment and managing the actor system lifecycle.
/**
* Default test configuration loaded from application-test.conf
*/
val ApplicationTestConfig: Config
/**
* Shutdown the test kit and underlying actor system
*/
def shutdownTestKit(): Unit
/**
* Get the underlying actor system
*/
implicit def system: ActorSystem[Nothing]
/**
* Get test kit settings with timeout and dilation configuration
*/
implicit def testKitSettings: TestKitSettings
/**
* Default timeout for ask operations
*/
implicit val timeout: Timeout
/**
* Access to system scheduler
*/
def scheduler: Scheduler