Class EventSourcedTestKit<S,E,ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S,E>>

Object
akka.javasdk.testkit.EventSourcedTestKit<S,E,ES>

public class EventSourcedTestKit<S,E,ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S,E>> extends Object
EventSourced Testkit for use in unit tests for EventSourced entities.

To test a EventSourced create a testkit instance by calling one of the available EventSourcedTestKit.of methods. The returned testkit is stateful, and it holds internally the state of the entity.

Use the call methods to interact with the testkit.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    final class 
     
    final class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    protected akka.javasdk.eventsourcedentity.EventSourcedEntity<S,E>
     
  • Method Summary

    Modifier and Type
    Method
    Description
    call(akka.japi.function.Function<ES,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use "method(MyEntity::myCommandHandler).invoke()" instead
    call(akka.japi.function.Function<ES,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func, akka.javasdk.Metadata metadata)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Use "method(MyEntity::myCommandHandler).withMetadata(metadata).invoke()" instead
    protected akka.javasdk.eventsourcedentity.EventSourcedEntity<S,E>
     
     
     
    protected final S
    handleEvent(S state, E event)
     
    protected <R> EventSourcedResult<R>
    interpretEffects(Supplier<akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> effect, String entityId, akka.javasdk.Metadata metadata, Optional<Type> returnType)
    creates a command context to run the commands, then creates an event context to run the events, and finally, creates a command context to run the side effects.
    boolean
     
    method(akka.japi.function.Function<ES,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func)
    Pass in an Event Sourced Entity command handler method reference without parameters, e.g.
    method(akka.japi.function.Function2<ES,I,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func)
    Pass in an Event Sourced Entity command handler method reference with a single parameter, e.g.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    of(String entityId, Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory)
    Creates a new testkit instance from a user defined entity id and a function EventSourcedEntityContext to EventSourcedEntity.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    of(String entityId, Supplier<ES> entityFactory)
    Creates a new testkit instance from a user defined entity id and an EventSourcedEntity Supplier.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    of(Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory)
    Creates a new testkit instance from a function EventSourcedEntityContext to EventSourcedEntity.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    of(Supplier<ES> entityFactory)
    Creates a new testkit instance from a EventSourcedEntity Supplier.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityFromEvents(String entityId, Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, List<E> initialEvents)
    Creates a new testkit instance from a user defined entity id, a factory function for EventSourcedEntity, and events from which to derive a state for the generated entity for tests.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityFromEvents(String entityId, Supplier<ES> entityFactory, List<E> initialEvents)
    Creates a new testkit instance from a user defined entity id, a Supplier of EventSourcedEntity, and events from which to derive a state for the generated entity for tests.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityFromEvents(Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, List<E> initialEvents)
    Creates a new testkit instance from a factory function for EventSourcedEntity and events from which to derive a state for the generated entity for tests.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityFromEvents(Supplier<ES> entityFactory, List<E> initialEvents)
    Creates a new testkit instance from a Supplier of EventSourcedEntity and events from which to derive a state for the generated entity.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityWithState(String entityId, Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, S initialState)
    Creates a new testkit instance from a user defined entity id, a factory function for EventSourcedEntity, and a state into which the built entity will be placed for tests.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityWithState(String entityId, Supplier<ES> entityFactory, S initialState)
    Creates a new testkit instance from a user defined entity id, a Supplier of EventSourcedEntity, and a state into which the supplied entity will be placed for tests.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityWithState(Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, S initialState)
    Creates a new testkit instance from a factory function for EventSourcedEntity and a state into which the built entity will be placed for tests.
    static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>>
    EventSourcedTestKit<S,E,ES>
    ofEntityWithState(Supplier<ES> entityFactory, S initialState)
    Creates a new testkit instance from a Supplier of EventSourcedEntity and a state.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_TEST_ENTITY_ID

      public static final String DEFAULT_TEST_ENTITY_ID
      See Also:
    • entity

      protected akka.javasdk.eventsourcedentity.EventSourcedEntity<S,E> entity
  • Method Details

    • of

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> of(Supplier<ES> entityFactory)
      Creates a new testkit instance from a EventSourcedEntity Supplier.

      A default test entity id will be automatically provided.

    • ofEntityWithState

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityWithState(Supplier<ES> entityFactory, S initialState)
      Creates a new testkit instance from a Supplier of EventSourcedEntity and a state.

      A default test entity id will be automatically provided.

    • ofEntityFromEvents

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityFromEvents(Supplier<ES> entityFactory, List<E> initialEvents)
      Creates a new testkit instance from a Supplier of EventSourcedEntity and events from which to derive a state for the generated entity.

      A default test entity id will be automatically provided.

    • of

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> of(Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory)
      Creates a new testkit instance from a function EventSourcedEntityContext to EventSourcedEntity.

      A default test entity id will be automatically provided.

    • ofEntityWithState

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityWithState(Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, S initialState)
      Creates a new testkit instance from a factory function for EventSourcedEntity and a state into which the built entity will be placed for tests.

      A default test entity id will be automatically provided.

    • ofEntityFromEvents

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityFromEvents(Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, List<E> initialEvents)
      Creates a new testkit instance from a factory function for EventSourcedEntity and events from which to derive a state for the generated entity for tests.

      A default test entity id will be automatically provided.

    • of

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> of(String entityId, Supplier<ES> entityFactory)
      Creates a new testkit instance from a user defined entity id and an EventSourcedEntity Supplier.
    • ofEntityWithState

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityWithState(String entityId, Supplier<ES> entityFactory, S initialState)
      Creates a new testkit instance from a user defined entity id, a Supplier of EventSourcedEntity, and a state into which the supplied entity will be placed for tests.
    • ofEntityFromEvents

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityFromEvents(String entityId, Supplier<ES> entityFactory, List<E> initialEvents)
      Creates a new testkit instance from a user defined entity id, a Supplier of EventSourcedEntity, and events from which to derive a state for the generated entity for tests.
    • of

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> of(String entityId, Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory)
      Creates a new testkit instance from a user defined entity id and a function EventSourcedEntityContext to EventSourcedEntity.
    • ofEntityWithState

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityWithState(String entityId, Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, S initialState)
      Creates a new testkit instance from a user defined entity id, a factory function for EventSourcedEntity, and a state into which the built entity will be placed for tests.
    • ofEntityFromEvents

      public static <S, E, ES extends akka.javasdk.eventsourcedentity.EventSourcedEntity<S, E>> EventSourcedTestKit<S,E,ES> ofEntityFromEvents(String entityId, Function<akka.javasdk.eventsourcedentity.EventSourcedEntityContext,ES> entityFactory, List<E> initialEvents)
      Creates a new testkit instance from a user defined entity id, a factory function for EventSourcedEntity, and events from which to derive a state for the generated entity for tests.
    • method

      public <R> EventSourcedTestKit<S,E,ES>.MethodRef<R> method(akka.japi.function.Function<ES,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func)
      Pass in an Event Sourced Entity command handler method reference without parameters, e.g. UserEntity::create
    • method

      public <I, R> EventSourcedTestKit<S,E,ES>.MethodRef1<I,R> method(akka.japi.function.Function2<ES,I,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func)
      Pass in an Event Sourced Entity command handler method reference with a single parameter, e.g. UserEntity::create
    • call

      @Deprecated(since="3.2.1", forRemoval=true) public <R> EventSourcedResult<R> call(akka.japi.function.Function<ES,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use "method(MyEntity::myCommandHandler).invoke()" instead
      The call method can be used to simulate a call to the EventSourcedEntity. The passed java lambda should return an EventSourcedEntity.Effect. The Effect is interpreted into an EventSourcedResult that can be used in test assertions.
      Type Parameters:
      R - The type of reply that is expected from invoking a command handler
      Parameters:
      func - A function from EventSourcedEntity to EventSourcedEntity.Effect.
      Returns:
      a EventSourcedResult
    • call

      @Deprecated(since="3.2.1", forRemoval=true) public <R> EventSourcedResult<R> call(akka.japi.function.Function<ES,akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> func, akka.javasdk.Metadata metadata)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use "method(MyEntity::myCommandHandler).withMetadata(metadata).invoke()" instead
      The call method can be used to simulate a call to the EventSourcedEntity. The passed java lambda should return an EventSourcedEntity.Effect. The Effect is interpreted into an EventSourcedResult that can be used in test assertions.
      Type Parameters:
      R - The type of reply that is expected from invoking a command handler
      Parameters:
      func - A function from EventSourcedEntity to EventSourcedEntity.Effect.
      metadata - A metadata passed as a call context.
      Returns:
      a EventSourcedResult
    • handleEvent

      protected final S handleEvent(S state, E event)
      Returns:
      The current state of the entity after applying the event
    • entity

      protected akka.javasdk.eventsourcedentity.EventSourcedEntity<S,E> entity()
    • getState

      public S getState()
      Returns:
      The current state of the entity
    • isDeleted

      public boolean isDeleted()
      Returns:
      true if the entity is deleted
    • getAllEvents

      public List<E> getAllEvents()
      Returns:
      All events persisted by command handlers of this entity up to now
    • interpretEffects

      protected <R> EventSourcedResult<R> interpretEffects(Supplier<akka.javasdk.eventsourcedentity.EventSourcedEntity.Effect<R>> effect, String entityId, akka.javasdk.Metadata metadata, Optional<Type> returnType)
      creates a command context to run the commands, then creates an event context to run the events, and finally, creates a command context to run the side effects. It cleans each context after each run.
      Returns:
      the result of the side effects