Package com.opencqrs.framework.command
Class CommandHandlingTestFixture<C extends Command>
java.lang.Object
com.opencqrs.framework.command.CommandHandlingTestFixture<C>
- Type Parameters:
C- the command type
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:
- given state initialization based on in-memory events and meta-data
Commandexecution to execute theCommandHandlerunder test- assertions to verify the command executed as expected, including verification of the events published by the command handler under test
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(...)
.given()
.nothing()
.when(new AddBookCommand(bookId, "Tolkien", "LOTR", "DE234723432"))
.succeeds()
.allEvents()
.exactly(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:
| 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 using GivenDsl.usingSubject(String) or per event using EventSpecifierDsl.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 EventSpecifierDsl.id(String) |
Event.time() |
is set to the value from GivenDsl.time(Instant), or Instant.now() by default, but can be overridden per event using EventSpecifierDsl.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 |
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder forCommandHandlingTestFixture.static classFluent API helper class for asserting captured events.static classFluent API helper class encapsulating the results of aCommandHandlerexecution for assertion. -
Method Summary
Modifier and TypeMethodDescriptiongiven()Initializes theCommandHandlingTestFixtureand returns aGivenDsl.Initialinstance for fluent state stubbing prior to command execution.static <I> CommandHandlingTestFixture.Builder<I> withStateRebuildingHandlerDefinitions(StateRebuildingHandlerDefinition<I, ?>... definitions) Creates aCommandHandlingTestFixture.Builderinstance for the givenStateRebuildingHandlerDefinitions.
-
Method Details
-
withStateRebuildingHandlerDefinitions
@SafeVarargs public static <I> CommandHandlingTestFixture.Builder<I> withStateRebuildingHandlerDefinitions(StateRebuildingHandlerDefinition<I, ?>... definitions) Creates aCommandHandlingTestFixture.Builderinstance for the givenStateRebuildingHandlerDefinitions.- Type Parameters:
I- the generic type of the instance to be event sourced before handling the command- Parameters:
definitions- theStateRebuildingHandlerDefinitions to be used to mimic event sourcing for theCommandHandlerunder test- Returns:
- a
CommandHandlingTestFixture.Builderinstance
-
given
Initializes theCommandHandlingTestFixtureand returns aGivenDsl.Initialinstance for fluent state stubbing prior to command execution.- Returns:
- a
GivenDsl.Initialinstance for further fluent API calls
-