Class ActorTestKit


  • public final class ActorTestKit
    extends java.lang.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 Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static com.typesafe.config.Config applicationTestConfig()
      Config loaded from application-test.conf, which is used if no specific config is given.
      static ActorTestKit create()
      Create a testkit named from the ActorTestKit class.
      static ActorTestKit create​(ActorSystem<?> system)
      Create a testkit from the provided actor system.
      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.
      static ActorTestKit create​(java.lang.String name)
      Create a testkit using the provided name.
      static ActorTestKit create​(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.
      static ActorTestKit create​(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.
      TestProbe<DeadLetter> createDeadLetterProbe()  
      TestProbe<Dropped> createDroppedMessageProbe()  
      <M> TestProbe<M> createTestProbe()
      Shortcut for creating a new test probe for the testkit actor system
      <M> TestProbe<M> createTestProbe​(java.lang.Class<M> clazz)
      Shortcut for creating a new test probe for the testkit actor system
      <M> TestProbe<M> createTestProbe​(java.lang.String name)
      Shortcut for creating a new named test probe for the testkit actor system
      <M> TestProbe<M> createTestProbe​(java.lang.String name, java.lang.Class<M> clazz)
      Shortcut for creating a new named test probe for the testkit actor system
      TestProbe<UnhandledMessage> createUnhandledMessageProbe()  
      Scheduler scheduler()
      The scheduler of the testkit actor system
      SerializationTestKit serializationTestKit()
      Additional testing utilities for serialization.
      static void shutdown​(ActorSystem<?> system)
      Shutdown the given 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).
      static void shutdown​(ActorSystem<?> system, java.time.Duration duration)
      Shutdown the given 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).
      static void shutdown​(ActorSystem<?> system, java.time.Duration duration, boolean throwIfShutdownTimesOut)
      Shutdown the given actor system and wait up to duration for shutdown to complete.
      void shutdownTestKit()
      Terminate the actor system and the testkit
      <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
      <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
      <T> ActorRef<T> spawn​(Behavior<T> behavior, java.lang.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.
      <T> ActorRef<T> spawn​(Behavior<T> behavior, java.lang.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.
      <T> void stop​(ActorRef<T> ref)
      Stop the actor under test and wait until it terminates.
      <T> void stop​(ActorRef<T> ref, java.time.Duration max)
      Stop the actor under test and wait max until it terminates.
      ActorSystem<java.lang.Void> system()
      The actor system running for this testkit.
      TestKitSettings testKitSettings()  
      Timeout timeout()
      The default timeout as specified with the config/TestKitSettings
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ActorTestKit

        public ActorTestKit​(ActorTestKit delegate)
    • Method Detail

      • 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​(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.

      • 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​(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().

      • create

        public static ActorTestKit create​(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,
                                    java.time.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,
                                    java.time.Duration duration)
        Shutdown the given 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 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.
      • system

        public ActorSystem<java.lang.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.
      • 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,
                                     java.lang.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,
                                     java.lang.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,
                             java.time.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​(java.lang.Class<M> clazz)
        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
      • createTestProbe

        public <M> TestProbe<M> createTestProbe​(java.lang.String name,
                                                java.lang.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.