or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

actor-refs.mdconfiguration.mdcore-testing.mddispatchers.mdevent-filtering.mdindex.mdjava-dsl.mdpackage-functions.mdsynchronization.mdtest-actors.mdutilities.md
tile.json

tessl/maven-com-typesafe-akka--akka-testkit_2-13

Akka TestKit - toolkit for testing Actor systems

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.typesafe.akka/akka-testkit_2.13@2.8.x

To install, run

npx @tessl/cli install tessl/maven-com-typesafe-akka--akka-testkit_2-13@2.8.0

index.mddocs/

Akka TestKit

A 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.

Package Information

  • Package Name: akka-testkit_2.13
  • Language: Scala
  • Package Type: Maven
  • Installation:
    // 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>

Core Imports

import akka.testkit._
import akka.actor.{ActorSystem, Props}
import scala.concurrent.duration._

For Java DSL:

import akka.testkit.javadsl.TestKit

Basic Usage

import 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")
    }
  }
}

Architecture

The Akka TestKit provides a layered testing architecture:

  • TestKit/TestProbe: Main testing classes providing comprehensive message expectations, timeouts, and assertion methods
  • TestActorRef/TestFSMRef: Direct access to actor instances and FSM states for unit testing
  • Event Filtering: Complete logging interception system including dead letter filtering
  • Synchronization Tools: Thread-safe barriers and latches for multi-threaded test coordination
  • Custom Dispatchers/Schedulers: Deterministic execution environments with manual time control
  • Test Utilities: Pre-built actors, socket utilities, and serialization helpers
  • Java DSL: Complete Java API with functional interface support

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.

Capabilities

Core Testing Framework

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 DefaultTimeout

Core Testing Framework

Actor References and Direct Testing

Specialized 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 TestFSMRef

Actor References and Direct Testing

Event Filtering and Logging

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 EventFilter

Event Filtering and Logging

Test Actors and Utilities

Pre-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 Actor

Test Actors and Utilities

Synchronization Utilities

Thread-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)

Synchronization Utilities

Custom Dispatchers and Scheduling

Specialized dispatchers and schedulers that provide deterministic execution environments for testing, including single-threaded execution and manual time advancement.

class CallingThreadDispatcher
class ExplicitlyTriggeredScheduler

Custom Dispatchers and Scheduling

Configuration and Extensions

TestKit configuration system and Akka extensions for customizing test behavior, timeouts, and environment settings.

object TestKitExtension extends ExtensionId[TestKitSettings]
class TestKitSettings(config: Config)

Configuration and Extensions

Helper Utilities

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 JavaSerializable

Helper Utilities

Java DSL

Complete 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.EventFilter

Java DSL

Package-Level Functions

Global 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)

Package-Level Functions