Class ActorTestKit


  • public final class ActorTestKit
    extends java.lang.Object
    Testkit for asynchronous testing of typed actors, meant for mixing into the test class.

    Provides a typed actor system started on creation, used for all test cases and 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 shutdown after all tests has run needs to be provided by the user.

    For synchronous testing of a Behavior see BehaviorTestKit

    • Method Detail

      • apply

        public static ActorTestKit apply()
        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.

      • apply

        public static ActorTestKit apply​(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.

      • apply

        public static ActorTestKit apply​(java.lang.String name)
        Create a testkit using the provided name.

        It will create an 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.

      • apply

        public static ActorTestKit apply​(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().

      • apply

        public static ActorTestKit apply​(java.lang.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 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().

      • apply

        public static ActorTestKit apply​(java.lang.String name,
                                         com.typesafe.config.Config customConfig,
                                         TestKitSettings settings)
        Create an 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 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)
        Shutdown the given ActorSystem and block until it shuts down, if more time than TestKitSettings.DefaultActorSystemShutdownTimeout passes an exception is thrown
      • shutdown

        public static void shutdown​(ActorSystem<?> system,
                                    scala.concurrent.duration.Duration timeout,
                                    boolean throwIfShutdownFails)
        Shutdown the given ActorSystem and block until it shuts down or the duration hits. If the timeout hits verifySystemShutdown decides
      • shutdown$default$3

        public static boolean shutdown$default$3()
      • ApplicationTestConfig

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

        public ActorSystem<?> internalSystem()
      • name

        public java.lang.String name()
      • config

        public com.typesafe.config.Config config()
      • system

        public ActorSystem<scala.runtime.Nothing$> system()
        INTERNAL API
      • timeout

        public Timeout timeout()
      • shutdownTestKit

        public void shutdownTestKit()
      • spawn

        public <T> ActorRef<T> spawn​(Behavior<T> behavior)
        Spawn the given behavior. This is created as a child of the test kit guardian
      • spawn

        public <T> ActorRef<T> spawn​(Behavior<T> behavior,
                                     Props props)
        Spawn the given behavior. This is created as a child of the test kit guardian
      • spawn

        public <T> ActorRef<T> spawn​(Behavior<T> behavior,
                                     java.lang.String name)
        Spawn the given behavior. This is created as a child of the test kit guardian
      • spawn

        public <T> ActorRef<T> spawn​(Behavior<T> behavior,
                                     java.lang.String name,
                                     Props props)
        Spawn the given behavior. This is created as a child of the test kit guardian
      • stop

        public <T> void stop​(ActorRef<T> ref,
                             scala.concurrent.duration.FiniteDuration max)
        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$default$2

        public <T> scala.concurrent.duration.FiniteDuration stop$default$2()
      • createTestProbe

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

        public <M> TestProbe<M> createTestProbe​(java.lang.String name)
        Shortcut for creating a new named test probe for the testkit actor system
      • 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.
      • 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.
      • 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.
      • serializationTestKit

        public SerializationTestKit serializationTestKit()
        Additional testing utilities for serialization.
      • systemActor

        public <T> ActorRef<T> systemActor​(Behavior<T> behavior,
                                           java.lang.String name)