Akka TestKit - toolkit for testing Actor systems
npx @tessl/cli install tessl/maven-com-typesafe-akka--akka-testkit_2-13@2.8.0A comprehensive testing toolkit specifically designed for testing Actor-based systems built with the Akka framework. It provides utilities and abstractions that make it easier to write unit and integration tests for actors, including features for testing actor messaging, FSM actors, event filtering, synchronization, time-controlled testing, and both synchronous and asynchronous testing patterns. The toolkit includes specialized components for message expectations, direct actor access, event filtering, synchronization primitives, custom dispatchers, and comprehensive Java interoperability.
// sbt
libraryDependencies += "com.typesafe.akka" %% "akka-testkit" % "2.8.8" % Test
// Maven
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.13</artifactId>
<version>2.8.8</version>
<scope>test</scope>
</dependency>import akka.testkit._
import akka.actor.{ActorSystem, Props}
import scala.concurrent.duration._For Java DSL:
import akka.testkit.javadsl.TestKitimport akka.actor.{Actor, ActorSystem, Props}
import akka.testkit.{TestKit, ImplicitSender}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import scala.concurrent.duration._
// Simple actor for testing
class EchoActor extends Actor {
def receive = {
case msg => sender() ! msg
}
}
// Test class extending TestKit
class EchoActorSpec extends TestKit(ActorSystem("TestSystem"))
with ImplicitSender
with WordSpecLike
with Matchers
with BeforeAndAfterAll {
override def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)
}
"An EchoActor" must {
"reply with the same message" in {
val echoActor = system.actorOf(Props[EchoActor]())
echoActor ! "hello"
expectMsg("hello")
}
"handle multiple messages" in {
val echoActor = system.actorOf(Props[EchoActor]())
echoActor ! "first"
echoActor ! "second"
expectMsg("first")
expectMsg("second")
}
}
}The Akka TestKit provides a layered testing architecture:
This architecture supports both unit testing (direct actor access) and integration testing (message-based interactions), enabling comprehensive verification of actor behavior, supervision hierarchies, and distributed system properties.
The primary TestKit class and related components for testing actor message interactions, including expectation methods, timeouts, and basic actor lifecycle testing.
class TestKit(system: ActorSystem)
trait TestKitBase
class TestProbe()(implicit system: ActorSystem)
trait ImplicitSender
trait DefaultTimeoutSpecialized actor references that provide direct access to actor internals and state, enabling unit testing of actor behavior without message passing.
class TestActorRef[T <: Actor](props: Props)(implicit system: ActorSystem)
class TestFSMRef[S, D, T <: Actor](props: Props)(implicit system: ActorSystem)
object TestActorRef
object TestFSMRefActor References and Direct Testing
Comprehensive system for intercepting and validating log events and exceptions during testing, allowing tests to assert expected logging behavior.
abstract class EventFilter
object EventFilter
class ErrorFilter extends EventFilter
class WarningFilter extends EventFilter
class InfoFilter extends EventFilter
class DebugFilter extends EventFilterPre-built actor implementations and factory methods for common testing patterns, including echo actors, blackhole actors, and message forwarding actors.
object TestActors
class EchoActor extends Actor
class BlackholeActor extends Actor
class ForwardActor(ref: ActorRef) extends ActorThread-safe synchronization primitives for coordinating multi-threaded tests and ensuring deterministic test execution across concurrent operations.
class TestBarrier(count: Int)
class TestLatch(count: Int = 1)Specialized dispatchers and schedulers that provide deterministic execution environments for testing, including single-threaded execution and manual time advancement.
class CallingThreadDispatcher
class ExplicitlyTriggeredSchedulerCustom Dispatchers and Scheduling
TestKit configuration system and Akka extensions for customizing test behavior, timeouts, and environment settings.
object TestKitExtension extends ExtensionId[TestKitSettings]
class TestKitSettings(config: Config)Additional utility functions and classes for common testing scenarios, including socket utilities, test exceptions, and serialization helpers.
object SocketUtil
case class TestException(message: String) extends RuntimeException
class TestJavaSerializer
trait JavaSerializableComplete Java API providing equivalent functionality to the Scala API with Java-friendly method signatures and type usage.
class akka.testkit.javadsl.TestKit(system: ActorSystem)
class akka.testkit.javadsl.EventFilterGlobal utility functions and implicit classes available at the package level for common testing operations and time dilation.
def filterEvents[T](eventFilters: EventFilter*)(block: => T)(implicit system: ActorSystem): T
def filterException[T <: Throwable](block: => Unit)(implicit system: ActorSystem, t: ClassTag[T]): Unit
implicit class TestDuration(duration: FiniteDuration)