Interface GivenDsl<C extends Command>
- Type Parameters:
C- the command type
- All Known Subinterfaces:
GivenDsl.Initial<C>
- Direct state injection via
state(Object) - Event-based state reconstruction via
events(Object...)orevent(Consumer) - Reusing events from another command execution via
command(CommandHandlingTestFixture, Command)
Events provided through this interface are processed by the configured StateRebuildingHandlerDefinitions
to build the instance state. These events are not included in the captured events that can be
asserted in the Expect phase - only events published by the command under test are capturable.
The interface supports method chaining, allowing multiple setup operations to be combined fluently:
fixture.given()
.time(Instant.parse("2024-01-01T10:00:00Z"))
.usingSubject("/orders/123")
.event(e -> e.payload(new OrderCreatedEvent(...)))
.timeDelta(Duration.ofHours(1))
.event(e -> e.payload(new OrderConfirmedEvent(...)))
.when(new ShipOrderCommand(...))
.succeeds();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceGivenDsl.Initial<C extends Command>Entry point for the Given phase, returned byCommandHandlingTestFixture.given().static interfaceGivenDsl.Terminated<C extends Command>Terminal state of the Given phase after callingGivenDsl.Initial.nothing(). -
Method Summary
Modifier and TypeMethodDescriptioncommand(CommandHandlingTestFixture<CMD> fixture, CMD command) Executes another command using the specified fixture and applies its captured events as given events for this test.command(CommandHandlingTestFixture<CMD> fixture, CMD command, Map<String, ?> metaData) Executes another command with meta-data using the specified fixture and applies its captured events as given events for this test.event(Consumer<EventSpecifierDsl> event) Adds a single event with full control over its attributes using theEventSpecifierDslfluent API.Adds multiple event payloads to be processed byStateRebuildingHandlerDefinitions.Directly sets the instance state without processing any events.Sets the timestamp for subsequent events added viaevents(Object...)orevent(Consumer).Advances the current timestamp by the specified duration for subsequent events.Sets the subject for subsequent events to the command's subject (as returned byCommand.getSubject()).usingSubject(String subject) Sets the subject to use for subsequent events added viaevents(Object...)orevent(Consumer).Completes the Given phase and transitions to the When phase by executing the specified command.Completes the Given phase and transitions to the When phase by executing the specified command with meta-data.
-
Method Details
-
time
Sets the timestamp for subsequent events added viaevents(Object...)orevent(Consumer). This timestamp is applied to the raw event's time passed toStateRebuildingHandlers.If not specified,
Instant.now()is used as the default timestamp.- Parameters:
time- the timestamp to use for subsequent events- Returns:
thisfor method chaining- See Also:
-
timeDelta
Advances the current timestamp by the specified duration for subsequent events. This is useful for simulating events that occurred at different points in time relative to each other.Example:
fixture.given() .time(Instant.parse("2024-01-01T10:00:00Z")) .event(e -> e.payload(new OrderPlacedEvent(...))) .timeDelta(Duration.ofHours(2)) .event(e -> e.payload(new PaymentReceivedEvent(...))) // at 12:00:00 .when(...)- Parameters:
delta- the duration to add to the current timestamp- Returns:
thisfor method chaining- See Also:
-
state
Directly sets the instance state without processing any events. This bypasses theStateRebuildingHandlerDefinitions and injects the state directly.Use this method when you want to test command behavior against a specific state without setting up the event history that would normally produce that state.
Note: This only affects preceding stubbings — subsequent
events(Object...)orevent(Consumer)calls can still be applied. However, if no events are added after this call, no events are present in the instance's history, which may affect precondition checks that rely on event existence (e.g.,Command.SubjectCondition.EXISTS).- Parameters:
state- the state object to inject- Returns:
thisfor method chaining
-
events
Adds multiple event payloads to be processed byStateRebuildingHandlerDefinitions. Each event payload is wrapped with default values for subject, time, id, and meta-data.The events are processed in the order provided, with each event potentially modifying the instance state through its corresponding
StateRebuildingHandler.For more control over individual event attributes (subject, time, meta-data), use
event(Consumer)instead.- Parameters:
events- the event payloads to add- Returns:
thisfor method chaining- Throws:
IllegalArgumentException- if noStateRebuildingHandlerDefinitionmatches an event's type- See Also:
-
event
Adds a single event with full control over its attributes using theEventSpecifierDslfluent API. This method allows specifying the event payload along with optional attributes like subject, time, id, and meta-data.Example:
fixture.given() .event(e -> e .payload(new OrderPlacedEvent(...)) .subject("/orders/123") .time(Instant.parse("2024-01-01T10:00:00Z")) .metaData(Map.of("correlationId", "abc-123"))) .when(...)- Parameters:
event- a consumer that configures the event viaEventSpecifierDsl- Returns:
thisfor method chaining- Throws:
IllegalArgumentException- ifEventSpecifierDsl.payload(Object)is not calledIllegalArgumentException- if noStateRebuildingHandlerDefinitionmatches the event's payload type- See Also:
-
command
Executes another command using the specified fixture and applies its captured events as given events for this test. This is useful for setting up complex preconditions that result from prior command executions.The events published by the given command are processed by this fixture's
StateRebuildingHandlerDefinitions to build the instance state.Note: The subject specified via
usingSubject(String)does not apply to events from the given command - they retain their original subjects as published by the command handler.- Type Parameters:
CMD- the command type- Parameters:
fixture- the fixture to execute the command withcommand- the command to execute- Returns:
thisfor method chaining- Throws:
AssertionError- if the given command execution fails
-
command
<CMD extends Command> GivenDsl<C> command(CommandHandlingTestFixture<CMD> fixture, CMD command, Map<String, ?> metaData) Executes another command with meta-data using the specified fixture and applies its captured events as given events for this test.- Type Parameters:
CMD- the command type- Parameters:
fixture- the fixture to execute the command withcommand- the command to executemetaData- the meta-data to pass to the command handler- Returns:
thisfor method chaining- Throws:
AssertionError- if the given command execution fails- See Also:
-
usingSubject
Sets the subject to use for subsequent events added viaevents(Object...)orevent(Consumer). This subject is used unless explicitly overridden per event usingEventSpecifierDsl.subject(String).Example:
fixture.given() .usingSubject("/orders/123") .events(new OrderPlacedEvent(...), new PaymentReceivedEvent(...)) // both use /orders/123 .usingSubject("/orders/456") .events(new OrderPlacedEvent(...)) // uses /orders/456 .when(...)- Parameters:
subject- the subject to use for subsequent events- Returns:
thisfor method chaining- See Also:
-
usingCommandSubject
Sets the subject for subsequent events to the command's subject (as returned byCommand.getSubject()). This is a convenience method equivalent tousingSubject(command.getSubject()).- Returns:
thisfor method chaining- See Also:
-
when
Completes the Given phase and transitions to the When phase by executing the specified command. The command is executed against the state built from the given events (or injected directly viastate(Object)).- Parameters:
command- the command to execute- Returns:
- an
ExpectDsl.Outcometo specify expected outcomes
-
when
Completes the Given phase and transitions to the When phase by executing the specified command with meta-data. The meta-data is passed to the command handler if it accepts meta-data (e.g.,CommandHandler.ForInstanceAndCommandAndMetaData).- Parameters:
command- the command to executemetaData- the meta-data to pass to the command handler- Returns:
- an
ExpectDsl.Outcometo specify expected outcomes
-