Class ActorTestKit

java.lang.Object
org.apache.pekko.actor.testkit.typed.javadsl.ActorTestKit

public final class ActorTestKit extends Object
Java API: Test kit for asynchronous testing of typed actors. Provides a typed actor system started on creation, that can be used for multiple test cases and is shut down when shutdown is called.

The actor system has a custom guardian that allows for spawning arbitrary actors using the spawn methods.

Designed to work with any test framework, but framework glue code that calls shutdownTestKit after all tests has run needs to be provided by the user or with TestKitJunitResource.

Use TestKit.create factories to construct manually or TestKitJunitResource to use together with JUnit tests

For synchronous testing of a Behavior see BehaviorTestKit

  • Method Details

    • create

      public static ActorTestKit create()
      Create a testkit named from the ActorTestKit class.

      When the test has completed you should terminate the ActorSystem and the testkit with shutdownTestKit().

      Config loaded from application-test.conf if that exists, otherwise using default configuration from the reference.conf resources that ship with the Akka libraries. The application.conf of your project is not used in this case.

    • create

      public static ActorTestKit create(ActorSystem<?> system)
      Create a testkit from the provided actor system.

      When the test has completed you should terminate the ActorSystem and the testkit with shutdownTestKit().

      Config loaded from the provided actor if that exists, otherwise using default configuration from the reference.conf resources that ship with the Akka libraries.

    • create

      public static ActorTestKit create(String name)
      Create a testkit using the provided name.

      It will create an pekko.actor.typed.ActorSystem with this name, e.g. threads will include the name. When the test has completed you should terminate the ActorSystem and the testkit with shutdownTestKit().

      Config loaded from application-test.conf if that exists, otherwise using default configuration from the reference.conf resources that ship with the Akka libraries. The application.conf of your project is not used in this case.

    • create

      public static ActorTestKit create(com.typesafe.config.Config customConfig)
      Create a testkit named from the ActorTestKit class, and use a custom config for the actor system.

      It will also used the provided customConfig provided to create the ActorSystem

      When the test has completed you should terminate the ActorSystem and the testkit with shutdownTestKit().

    • create

      public static ActorTestKit create(String name, com.typesafe.config.Config customConfig)
      Create a test kit named based on the provided name, and uses the provided custom config for the actor system.

      It will create an pekko.actor.typed.ActorSystem with this name, e.g. threads will include the name.

      It will also used the provided customConfig provided to create the ActorSystem

      When the test has completed you should terminate the ActorSystem and the testkit with shutdownTestKit().

    • create

      public static ActorTestKit create(String name, com.typesafe.config.Config customConfig, TestKitSettings settings)
      Create an pekko.actor.typed.ActorSystem named based on the provided name, use the provided custom config for the actor system, and the testkit will use the provided setting.

      It will create an pekko.actor.typed.ActorSystem with this name, e.g. threads will include the name.

      It will also used the provided customConfig provided to create the ActorSystem, and provided setting.

      When the test has completed you should terminate the ActorSystem and the testkit with shutdownTestKit().

    • shutdown

      public static void shutdown(ActorSystem<?> system, Duration duration, boolean throwIfShutdownTimesOut)
      Shutdown the given actor system and wait up to duration for shutdown to complete.
      Parameters:
      throwIfShutdownTimesOut - Fail the test if the system fails to shut down, if false an error is printed to stdout when the system did not shutdown but no exception is thrown.
    • shutdown

      public static void shutdown(ActorSystem<?> system, Duration duration)
      Shutdown the given pekko.actor.typed.ActorSystem and block until it shuts down, if more time than system-shutdown-default passes an exception is thrown (can be configured with throw-on-shutdown-timeout).
    • shutdown

      public static void shutdown(ActorSystem<?> system)
      Shutdown the given pekko.actor.typed.ActorSystem and block until it shuts down, if more time than system-shutdown-default passes an exception is thrown (can be configured with throw-on-shutdown-timeout).
    • applicationTestConfig

      public static com.typesafe.config.Config applicationTestConfig()
      Config loaded from application-test.conf, which is used if no specific config is given.
    • timeout

      public Timeout timeout()
      The default timeout as specified with the config/pekko.actor.testkit.typed.TestKitSettings
    • system

      public ActorSystem<Void> system()
      The actor system running for this testkit. Interaction with the user guardian is done through methods on the testkit which is why it is typed to Void.
    • testKitSettings

      public TestKitSettings testKitSettings()
    • scheduler

      public Scheduler scheduler()
      The scheduler of the testkit actor system
    • spawn

      public <T> ActorRef<T> spawn(Behavior<T> behavior)
      Spawn a new auto-named actor under the testkit user guardian and return the ActorRef for the spawned actor
    • spawn

      public <T> ActorRef<T> spawn(Behavior<T> behavior, String name)
      Spawn a new named actor under the testkit user guardian and return the ActorRef for the spawned actor, note that spawning actors with the same name in multiple test cases will cause failures.
    • spawn

      public <T> ActorRef<T> spawn(Behavior<T> behavior, Props props)
      Spawn a new auto-named actor under the testkit user guardian with the given props and return the ActorRef for the spawned actor
    • spawn

      public <T> ActorRef<T> spawn(Behavior<T> behavior, String name, Props props)
      Spawn a new named actor under the testkit user guardian with the given props and return the ActorRef for the spawned actor, note that spawning actors with the same name in multiple test cases will cause failures.
    • stop

      public <T> void stop(ActorRef<T> ref)
      Stop the actor under test and wait until it terminates. It can only be used for actors that were spawned by this ActorTestKit. Other actors will not be stopped by this method.
    • stop

      public <T> void stop(ActorRef<T> ref, Duration max)
      Stop the actor under test and wait max until it terminates. It can only be used for actors that were spawned by this ActorTestKit. Other actors will not be stopped by this method.
    • createTestProbe

      public <M> TestProbe<M> createTestProbe()
      Shortcut for creating a new test probe for the testkit actor system
    • createTestProbe

      public <M> TestProbe<M> createTestProbe(Class<M> clazz)
      Shortcut for creating a new test probe for the testkit actor system
    • createTestProbe

      public <M> TestProbe<M> createTestProbe(String name)
      Shortcut for creating a new named test probe for the testkit actor system
    • createTestProbe

      public <M> TestProbe<M> createTestProbe(String name, Class<M> clazz)
      Shortcut for creating a new named test probe for the testkit actor system
    • createDroppedMessageProbe

      public TestProbe<Dropped> createDroppedMessageProbe()
      Returns:
      A test probe that is subscribed to dropped letters from the system event bus. Subscription will be completed and verified so any dropped letter after it will be caught by the probe.
    • createDeadLetterProbe

      public TestProbe<DeadLetter> createDeadLetterProbe()
      Returns:
      A test probe that is subscribed to dead letters from the system event bus. Subscription will be completed and verified so any dead letter after it will be caught by the probe.
    • createUnhandledMessageProbe

      public TestProbe<UnhandledMessage> createUnhandledMessageProbe()
      Returns:
      A test probe that is subscribed to unhandled messages from the system event bus. Subscription will be completed and verified so any unhandled message after it will be caught by the probe.
    • shutdownTestKit

      public void shutdownTestKit()
      Terminate the actor system and the testkit
    • serializationTestKit

      public SerializationTestKit serializationTestKit()
      Additional testing utilities for serialization.