Class CommandHandlingTestFixture<I,C extends Command,R>
- Type Parameters:
I
- the generic type of the instance to be event sourced before handling the commandC
- the command typeR
- the command execution result type
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 StateRebuildingHandlerDefinition
s. 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
Command
execution to execute theCommandHandler
under 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 StateRebuildingHandlerDefinition
s 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 Event
s, 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 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 |
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Builder forCommandHandlingTestFixture
.static class
Fluent API helper class for asserting a captured events.class
Fluent API helper class encapsulating the results of aCommandHandler
execution for assertion.class
CommandHandlingTestFixture.Given<C extends Command>
Fluent API helper class encapsulating the current state stubbing prior to executing theCommandHandler
under test. -
Method Summary
Modifier and TypeMethodDescriptionInitializes theCommandHandlingTestFixture
with the given event payloads applied in order to the configuredStateRebuildingHandlerDefinition
s to reconstruct the instance state.Initializes theCommandHandlingTestFixture
using theCommandHandlingTestFixture.Given.GivenEvent
consumer for more fine-grained event specification of the events and their meta-data, which will be applied in order to the configuredStateRebuildingHandlerDefinition
s to reconstruct the instance state.givenCommand
(CommandHandlingTestFixture<I, AnotherCommand, ?> fixture, AnotherCommand command) Execute the givenCommand
without meta-data using theCommandHandlerDefinition
encapsulated within the given fixture to capture any new events published, which in turn will be applied tothis
.givenCommand
(CommandHandlingTestFixture<I, AnotherCommand, ?> fixture, AnotherCommand command, Map<String, ?> metaData) Execute the givenCommand
with meta-data using theCommandHandlerDefinition
encapsulated within the given fixture to capture any new events published, which in turn will be applied tothis
.Initializes theCommandHandlingTestFixture
with no prior state.givenState
(I state) Initializes theCommandHandlingTestFixture
with the given instance state.Initializes theCommandHandlingTestFixture
with no prior state, but a specific time-stamp.usingSubject
(String subject) Specifies theEvent.subject()
to be used for subsequent calls toCommandHandlingTestFixture.Given.andGiven(Object...)
.static <I> CommandHandlingTestFixture.Builder
<I> withStateRebuildingHandlerDefinitions
(StateRebuildingHandlerDefinition<I, ?>... definitions) Creates aCommandHandlingTestFixture.Builder
instance for the givenStateRebuildingHandlerDefinition
s.
-
Method Details
-
withStateRebuildingHandlerDefinitions
public static <I> CommandHandlingTestFixture.Builder<I> withStateRebuildingHandlerDefinitions(StateRebuildingHandlerDefinition<I, ?>... definitions) Creates aCommandHandlingTestFixture.Builder
instance for the givenStateRebuildingHandlerDefinition
s.- Type Parameters:
I
- the generic type of the instance to be event sourced before handling the command- Parameters:
definitions
- theStateRebuildingHandlerDefinition
s to be used to mimic event sourcing for theCommandHandler
under test- Returns:
- a
CommandHandlingTestFixture.Builder
instance
-
givenNothing
Initializes theCommandHandlingTestFixture
with no prior state. This should be used for testingCommandHandler
s using a pristine subject.- Returns:
- a
CommandHandlingTestFixture.Given
instance for further fluent API calls
-
givenTime
Initializes theCommandHandlingTestFixture
with no prior state, but a specific time-stamp. This should be used, if further events shall be applied with thatEvent.time()
.- Parameters:
time
- the initialization time-stamp- Returns:
- a
CommandHandlingTestFixture.Given
instance for further fluent API calls
-
givenState
Initializes theCommandHandlingTestFixture
with the given instance state. This can be used in favor of the preferred event based initialization, if the latter one is too complex, for instance requiring too many events.- Parameters:
state
- the initialization state- Returns:
- a
CommandHandlingTestFixture.Given
instance for further fluent API calls
-
usingSubject
Specifies theEvent.subject()
to be used for subsequent calls toCommandHandlingTestFixture.Given.andGiven(Object...)
.This is a convenience method to be used in favor of specifying event subjects using
CommandHandlingTestFixture.Given.GivenEvent.subject(String)
, which, if used, however, takes precedence over the subject defined using this method. Notice, that the event subject specified by this method does not affect calls toCommandHandlingTestFixture.Given.andGivenCommand(CommandHandlingTestFixture, Command)
.- Parameters:
subject
- the event subject to be applied to any subsequent given events- Returns:
- a
CommandHandlingTestFixture.Given
instance for further fluent API calls - See Also:
-
given
Initializes theCommandHandlingTestFixture
with the given event payloads applied in order to the configuredStateRebuildingHandlerDefinition
s to reconstruct the instance state.- Parameters:
events
- the events to be applied- Returns:
- a
CommandHandlingTestFixture.Given
instance for further fluent API calls
-
given
public CommandHandlingTestFixture<I,C, givenR>.Given<C> (Consumer<CommandHandlingTestFixture.Given.GivenEvent<I>> event) Initializes theCommandHandlingTestFixture
using theCommandHandlingTestFixture.Given.GivenEvent
consumer for more fine-grained event specification of the events and their meta-data, which will be applied in order to the configuredStateRebuildingHandlerDefinition
s to reconstruct the instance state.- Parameters:
event
- event specification consumer, at leastCommandHandlingTestFixture.Given.GivenEvent.payload(Object)
must be called- Returns:
- a
CommandHandlingTestFixture.Given
instance for further fluent API calls
-
givenCommand
public <AnotherCommand extends Command> CommandHandlingTestFixture<I,C, givenCommandR>.Given<C> (CommandHandlingTestFixture<I, AnotherCommand, ?> fixture, AnotherCommand command) Execute the givenCommand
without meta-data using theCommandHandlerDefinition
encapsulated within the given fixture to capture any new events published, which in turn will be applied tothis
. This is mostly used forCommandHandler
s publishing a lot or more complex events, in favor of stubbing the events directly.Be aware that stubbed events can be specified more precisely than captured ones, since the encapsulated
CommandHandler
is responsible for event publication using theCommandEventPublisher
. Hence,CommandHandlingTestFixture.Given.GivenEvent.time(Instant)
andCommandHandlingTestFixture.Given.GivenEvent.id(String)
cannot be specified using this approach.- Type Parameters:
AnotherCommand
- generic command type to execute- Parameters:
fixture
- the fixture holding the command to executecommand
- the command to execute for event capturing- Returns:
- a
this
for further fluent API calls - Throws:
AssertionError
- in case the given command did not execute successfully
-
givenCommand
public <AnotherCommand extends Command> CommandHandlingTestFixture<I,C, givenCommandR>.Given<C> (CommandHandlingTestFixture<I, AnotherCommand, ?> fixture, AnotherCommand command, Map<String, ?> metaData) Execute the givenCommand
with meta-data using theCommandHandlerDefinition
encapsulated within the given fixture to capture any new events published, which in turn will be applied tothis
. This is mostly used forCommandHandler
s publishing a lot or more complex events, in favor of stubbing the events directly.Be aware that stubbed events can be specified more precisely than captured ones, since the encapsulated
CommandHandler
is responsible for event publication using theCommandEventPublisher
. Hence,CommandHandlingTestFixture.Given.GivenEvent.time(Instant)
andCommandHandlingTestFixture.Given.GivenEvent.id(String)
cannot be specified using this approach.- Type Parameters:
AnotherCommand
- generic command type to execute- Parameters:
fixture
- the fixture holding the command to executecommand
- the command to execute for event capturingmetaData
- the command meta-data- Returns:
- a
this
for further fluent API calls - Throws:
AssertionError
- in case the given command did not execute successfully
-