Class CommandHandlingTestFixture<I,C extends Command,R>

java.lang.Object
com.opencqrs.framework.command.CommandHandlingTestFixture<I,C,R>
Type Parameters:
I - the generic type of the instance to be event sourced before handling the command
C - the command type
R - the command execution result type

public class CommandHandlingTestFixture<I,C extends Command,R> extends Object
Test support for CommandHandler or CommandHandlerDefinition. This class can be used in favor of the CommandRouter to test command handling logic without interacting with the event store, solely relying on a set of StateRebuildingHandlerDefinitions. No event upcasting, event type resolution, or meta-data propagation is involved during test execution.

This class follows the Given When Then style of representing tests with a fluent API supporting:

  1. given state initialization based on in-memory events and meta-data
  2. Command execution to execute the CommandHandler under test
  3. assertions to verify the command executed as expected, including verification of the events published by the command handler under test
A typical test case using this may look as follows. The StateRebuildingHandlerDefinitions needed to mimic event sourcing as well as the CommandHandler definition under test have been omitted for brevity.
     @Test
     public void bookAdded() {
          UUID bookId = UUID.randomUUID();
          CommandHandlingTestFixture
              // specify state rebuilding handler definitions to use
              .withStateRebuildingHandlerDefinitions(...)
              // specify command handler (definition) to test
              .using(...)
              .givenNothing()
              .when(
                  new AddBookCommand(
                      bookId,
                      "Tolkien",
                      "LOTR",
                      "DE234723432"
                  )
              )
              .expectSuccessfulExecution()
              .expectSingleEvent(
                  new BookAddedEvent(
                      bookId,
                      "Tolkien",
                      "LOTR",
                      "DE234723432"
                  )
              );
     }
 
In lack of the event store, for StateRebuildingHandler.FromObjectAndRawEvent.on(Object, Object, Event) and StateRebuildingHandler.FromObjectAndMetaDataAndSubjectAndRawEvent.on(Object, Object, Map, String, Event) the given state initialization uses stubbed raw Events, instead, based on the following contents:
Raw event stubbing
event attribute value derivation
Event.source() is set to a fixed value and cannot be overridden
Event.subject() is set to Command.getSubject(), but can be overridden per event using CommandHandlingTestFixture.Given.GivenEvent.subject(String)
Event.type() is set to a fixed value and cannot be overridden
Event.data() is set to an empty map and cannot be overridden
Event.specVersion() is set to a fixed value and cannot be overridden
Event.id() is set randomly, but can be overridden per event using CommandHandlingTestFixture.Given.GivenEvent.id(String)
Event.time() is set to the value from givenTime(Instant), or CommandHandlingTestFixture.Given.andGivenTime(Instant), or Instant.now() by default, but can be overridden per event using CommandHandlingTestFixture.Given.GivenEvent.time(Instant)
Event.dataContentType() is set to a fixed value and cannot be overridden
Event.hash() is set to a random value and cannot be overridden
Event.predecessorHash() is set to a random value and cannot be overridden