Class TestKit

Object
akka.javasdk.testkit.TestKit

public class TestKit extends Object
Testkit for running services locally.

Create a TestKit and then start() the testkit before testing the service with HTTP clients. Call stop() after tests are complete.

  • Constructor Details

    • TestKit

      public TestKit()
      Create a new testkit for a service descriptor with the default settings.
    • TestKit

      public TestKit(TestKit.Settings settings)
      Create a new testkit for a service descriptor with custom settings.
      Parameters:
      settings - custom testkit settings
  • Method Details

    • start

      public TestKit start()
      Start this testkit with default configuration. The default configuration is loaded from application-test.conf if that exists, otherwise from application.conf.
      Returns:
      this TestKit instance
    • getHost

      public String getHost()
      Get the host name/IP address where the service is available. This is relevant in certain Continuous Integration environments.
    • getPort

      public int getPort()
      Get the local port where the service is available.
    • getApplicationConfig

      public com.typesafe.config.Config getApplicationConfig()
      Returns:
      The config as the components of the service under test sees it, if injected.
    • getMaterializer

      public akka.stream.Materializer getMaterializer()
      An Akka Stream materializer to use for running streams. Needed for example in a command handler which accepts streaming elements but returns a single async reply once all streamed elements has been consumed.
    • getActorSystem

      public akka.actor.typed.ActorSystem<?> getActorSystem()
      Get an ActorSystem for creating Akka HTTP clients.
      Returns:
      test actor system
    • getComponentClient

      public akka.javasdk.client.ComponentClient getComponentClient()
      Get an ComponentClient for interacting "internally" with the components of a service.
    • getTimerScheduler

      public akka.javasdk.timer.TimerScheduler getTimerScheduler()
      Get a TimerScheduler for scheduling TimedAction.
    • getHttpClientProvider

      public akka.javasdk.http.HttpClientProvider getHttpClientProvider()
      Get a HttpClientProvider for looking up HTTP clients to interact with other services than the current. Requests will appear as coming from this service from an ACL perspective.
    • getGrpcEndpointClient

      public <T extends akka.grpc.javadsl.AkkaGrpcClient> T getGrpcEndpointClient(Class<T> grpcClientClass)
      Get a gRPC client for an endpoint provided by this service. Requests will appear as coming from this service itself from an ACL perspective.
      Parameters:
      grpcClientClass - The generated Akka gRPC client interface for a gRPC endpoint in this service
    • getGrpcEndpointClient

      public <T extends akka.grpc.javadsl.AkkaGrpcClient> T getGrpcEndpointClient(Class<T> grpcClientClass, akka.javasdk.Principal requestPrincipal)
      Get a gRPC client for an endpoint provided by this service but specify the client principal for the ACLs.
      Parameters:
      grpcClientClass - The generated Akka gRPC client interface for a gRPC endpoint in this service
      requestPrincipal - A principal that any request from the returned service will have when requests are handled in the endpoint.
    • getSelfHttpClient

      public akka.javasdk.http.HttpClient getSelfHttpClient()
      Get a HttpClient for interacting with the service itself, the client will not be authenticated and will appear to the service as a request with the internet principal.
    • getKeyValueEntityIncomingMessages

      @Deprecated(since="3.4.2", forRemoval=true) public EventingTestKit.IncomingMessages getKeyValueEntityIncomingMessages(String componentId)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Get incoming messages for KeyValueEntity.
      Parameters:
      componentId - As annotated with @Component on the KeyValueEntity
    • getKeyValueEntityIncomingMessages

      public EventingTestKit.IncomingMessages getKeyValueEntityIncomingMessages(Class<? extends akka.javasdk.keyvalueentity.KeyValueEntity<?>> keyValueEntityClass)
    • getEventSourcedEntityIncomingMessages

      @Deprecated(since="3.4.2", forRemoval=true) public EventingTestKit.IncomingMessages getEventSourcedEntityIncomingMessages(String componentId)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Get incoming messages for EventSourcedEntity.
      Parameters:
      componentId - As annotated with @Component on the EventSourcedEntity
    • getEventSourcedEntityIncomingMessages

      public EventingTestKit.IncomingMessages getEventSourcedEntityIncomingMessages(Class<? extends akka.javasdk.eventsourcedentity.EventSourcedEntity<?,?>> eventSourcedEntityClass)
    • getComponentIdValue

      public static String getComponentIdValue(Class<?> componentClass)
      Utility method to get the component ID from a component class.
      Parameters:
      componentClass - the component class annotated with @Component or @ComponentId
      Returns:
      the component ID
    • getComponentName

      public static String getComponentName(Class<?> componentClass)
      Utility method to get the component name from a component class.
      Parameters:
      componentClass - the component class annotated with @Component
      Returns:
      the component name, or null if not specified or using @ComponentId
    • getComponentDescription

      public static String getComponentDescription(Class<?> componentClass)
      Utility method to get the component description from a component class.
      Parameters:
      componentClass - the component class annotated with @Component
      Returns:
      the component description, or null if not specified or using @ComponentId
    • getWorkflowIncomingMessages

      @Deprecated(since="3.4.2", forRemoval=true) public EventingTestKit.IncomingMessages getWorkflowIncomingMessages(String componentId)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Get incoming messages for Workflow.
      Parameters:
      componentId - As annotated with @Component on the EventSourcedEntity
    • getWorkflowIncomingMessages

      public EventingTestKit.IncomingMessages getWorkflowIncomingMessages(Class<? extends akka.javasdk.workflow.Workflow<?>> workflowClass)
    • getStreamIncomingMessages

      public EventingTestKit.IncomingMessages getStreamIncomingMessages(String service, String streamId)
      Get incoming messages for Consume.ServiceStream.
      Parameters:
      service - service name
      streamId - service stream id
    • getTopicIncomingMessages

      public EventingTestKit.IncomingMessages getTopicIncomingMessages(String topic)
      Get incoming messages for Topic.
      Parameters:
      topic - topic name
    • getTopicOutgoingMessages

      public EventingTestKit.OutgoingMessages getTopicOutgoingMessages(String topic)
      Get mocked topic destination.
      Parameters:
      topic - topic name
    • getAgentRegistry

      public akka.javasdk.agent.AgentRegistry getAgentRegistry()
    • getSanitizer

      public akka.javasdk.Sanitizer getSanitizer()
      Returns:
      The configured sanitizer for the service, for test assertions that the expected anonymization is applied. Will always return an instance, if no sanitization rules are configured, the returned sanitizer will return all text fed to it as is.
    • stop

      public void stop()
      Stop the testkit and local runtime.
    • availableLocalPort

      public static int availableLocalPort()
      Get an available local port for testing.
      Returns:
      available local port
    • getMessageBuilder

      public EventingTestKit.MessageBuilder getMessageBuilder()
      Returns EventingTestKit.MessageBuilder utility to create EventingTestKit.Messages for the eventing testkit.
    • getDependencyProvider

      public Optional<akka.javasdk.DependencyProvider> getDependencyProvider()
      Returns:
      The custom dependency provider used in this test, if one is defined, when overriding the dependency provided through TestKit.Settings.withDependencyProvider(DependencyProvider) the overridden provider is returned.