public class SagaTestFixture<T> extends Object implements FixtureConfiguration, ContinuedGivenState
| Constructor and Description |
|---|
SagaTestFixture(Class<T> sagaType)
Creates an instance of the AnnotatedSagaTestFixture to test sagas of the given
sagaType. |
| Modifier and Type | Method and Description |
|---|---|
GivenAggregateEventPublisher |
andThenAggregate(String aggregateIdentifier)
Use this method to indicate that an aggregate with given identifier published certain events.
|
ContinuedGivenState |
andThenAPublished(Object event)
Indicates that the given
event has been published in the past. |
ContinuedGivenState |
andThenAPublished(Object event,
Map<String,?> metaData)
Indicates that the given
event with given metaData has been published in the past. |
ContinuedGivenState |
andThenTimeAdvancesTo(Instant newDateTime)
Simulate time shifts in the current given state.
|
ContinuedGivenState |
andThenTimeElapses(Duration elapsedTime)
Simulate time shifts in the current given state.
|
Instant |
currentTime()
Returns the time as "known" by the fixture.
|
protected void |
ensureSagaResourcesInitialized()
Initializes the saga resources if it hasn't already done so.
|
RecordingCommandBus |
getCommandBus()
Returns the command bus used by this fixture.
|
EventBus |
getEventBus()
Returns the event bus used by this fixture.
|
GivenAggregateEventPublisher |
givenAggregate(String aggregateIdentifier)
Use this method to indicate that an aggregate with given identifier published certain events.
|
ContinuedGivenState |
givenAPublished(Object event)
Indicates that the given
applicationEvent has been published in the past. |
ContinuedGivenState |
givenCurrentTime(Instant currentTime)
Use this method to indicate a specific moment as the initial current time "known" by the fixture at the start of
the given state.
|
WhenState |
givenNoPriorActivity()
Indicates that no relevant activity has occurred in the past.
|
protected void |
handleDeadline(ScopeDescriptor sagaDescriptor,
DeadlineMessage<?> deadlineMessage)
Handles the given
deadlineMessage in the saga described by the given sagaDescriptor. |
protected void |
handleInSaga(EventMessage<?> event)
Handles the given
event in the scope of a Unit of Work. |
<I> I |
registerCommandGateway(Class<I> gatewayInterface)
Creates a Command Gateway for the given
gatewayInterface and registers that as a resource. |
<I> I |
registerCommandGateway(Class<I> gatewayInterface,
I stubImplementation)
Creates a Command Gateway for the given
gatewayInterface and registers that as a resource. |
FixtureConfiguration |
registerDeadlineDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage<?>> deadlineDispatchInterceptor)
Registers a deadline dispatch interceptor which will always be invoked before a deadline is dispatched
(scheduled) on the
DeadlineManager to perform a task specified in the
interceptor. |
FixtureConfiguration |
registerDeadlineHandlerInterceptor(MessageHandlerInterceptor<? super DeadlineMessage<?>> deadlineHandlerInterceptor)
Registers a deadline handler interceptor which will always be invoked before a deadline is handled to perform a
task specified in the interceptor.
|
FixtureConfiguration |
registerEventHandlerInterceptor(MessageHandlerInterceptor<? super EventMessage<?>> eventHandlerInterceptor)
Registers a
MessageHandlerInterceptor for EventMessages. |
FixtureConfiguration |
registerFieldFilter(FieldFilter fieldFilter)
Registers the given
fieldFilter, which is used to define which Fields are used when comparing objects. |
FixtureConfiguration |
registerHandlerDefinition(HandlerDefinition handlerDefinition)
Registers a
HandlerDefinition within this fixture. |
FixtureConfiguration |
registerHandlerEnhancerDefinition(HandlerEnhancerDefinition handlerEnhancerDefinition)
Registers a
HandlerEnhancerDefinition within this fixture. |
FixtureConfiguration |
registerIgnoredField(Class<?> declaringClass,
String fieldName)
Indicates that a field with given
fieldName, which is declared in given declaringClass is ignored
when performing deep equality checks. |
FixtureConfiguration |
registerListenerInvocationErrorHandler(ListenerInvocationErrorHandler listenerInvocationErrorHandler)
Registers a
ListenerInvocationErrorHandler to be set for the Saga to deal with exceptions being thrown from within Saga Event Handlers. |
FixtureConfiguration |
registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory)
Registers a
ParameterResolverFactory within this fixture. |
void |
registerResource(Object resource)
Registers the given
resource. |
FixtureConfiguration |
registerResourceInjector(ResourceInjector resourceInjector)
Registers a
ResourceInjector within this fixture. |
FixtureConfiguration |
registerStartRecordingCallback(Runnable onStartRecordingCallback)
Registers a callback to be invoked when the fixture execution starts recording.
|
void |
setCallbackBehavior(CallbackBehavior callbackBehavior)
Sets the instance that defines the behavior of the Command Bus when a command is dispatched with a callback.
|
FixtureConfiguration |
suppressExceptionInGivenPhase(boolean suppress)
Configure whether the fixture should suppress exceptions thrown during the given-phase.
|
WhenAggregateEventPublisher |
whenAggregate(String aggregateIdentifier)
Use this method to indicate that an aggregate with given identifier should publish certain events, while
recording the outcome.
|
FixtureExecutionResult |
whenPublishingA(Object event)
Use this method to indicate an application is published, while recording the outcome.
|
FixtureExecutionResult |
whenPublishingA(Object event,
Map<String,?> metaData)
Use this method to indicate an application is published with given additional
metaData,
while recording the outcome. |
FixtureExecutionResult |
whenTimeAdvancesTo(Instant newDateTime)
Mimic an elapsed time with no relevant activity for the Saga.
|
FixtureExecutionResult |
whenTimeElapses(Duration elapsedTime)
Mimic an elapsed time with no relevant activity for the Saga.
|
FixtureConfiguration |
withTransienceCheckDisabled()
Disables the check that injected resources are stored in fields that are marked 'transient'.
|
protected void handleInSaga(EventMessage<?> event)
event in the scope of a Unit of Work. If handling the event results in an exception the
exception will be wrapped in a FixtureExecutionException.event - The event message to handleprotected void handleDeadline(ScopeDescriptor sagaDescriptor, DeadlineMessage<?> deadlineMessage) throws Exception
deadlineMessage in the saga described by the given sagaDescriptor. Deadline
message is handled in the scope of a UnitOfWork. If handling the
deadline results in an exception, the exception will be wrapped in a FixtureExecutionException.sagaDescriptor - A ScopeDescriptor describing the saga under testdeadlineMessage - The DeadlineMessage to be handledExceptionprotected void ensureSagaResourcesInitialized()
public FixtureConfiguration withTransienceCheckDisabled()
FixtureConfigurationBy default, Saga fixtures check for the transient modifier on fields that hold injected resources. These resources are generally not means to be serialized as part of the Saga.
When the transience check reports false positives, this method allows this check to be skipped.
withTransienceCheckDisabled in interface FixtureConfigurationpublic FixtureExecutionResult whenTimeElapses(Duration elapsedTime)
WhenStateFixtureConfiguration.registerResource(Object), you may need to
reset them yourself if they are manipulated by the Saga in the "given" stage of the test.whenTimeElapses in interface WhenStateelapsedTime - The amount of time to elapsepublic FixtureExecutionResult whenTimeAdvancesTo(Instant newDateTime)
WhenStateFixtureConfiguration.registerResource(Object), you may need to
reset them yourself if they are manipulated by the Saga in the "given" stage of the test.whenTimeAdvancesTo in interface WhenStatenewDateTime - The time to advance the clock topublic void registerResource(Object resource)
FixtureConfigurationresource. When a Saga is created, all resources are injected on that instance before
any Events are passed onto it.
Note that a CommandBus, EventBus and EventScheduler are already registered as resources, and need not be
registered again.
Also note that you might need to reset the resources manually if you want to isolate behavior during the "when"
stage of the test.registerResource in interface FixtureConfigurationresource - the resource to register.public FixtureConfiguration registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory)
FixtureConfigurationParameterResolverFactory within this fixture. The given parameterResolverFactory will
be added to the other parameter resolver factories introduced through ClasspathParameterResolverFactory.forClass(Class) and the SimpleResourceParameterResolverFactory adding the registered resources
(with FixtureConfiguration.registerResource(Object). The type of the saga under test is used as input for the ClasspathParameterResolverFactory#forClass(Class) operation.registerParameterResolverFactory in interface FixtureConfigurationparameterResolverFactory - the ParameterResolver to register within this fixtureFixtureConfiguration.registerResource(Object)public void setCallbackBehavior(CallbackBehavior callbackBehavior)
FixtureConfigurationsetCallbackBehavior in interface FixtureConfigurationcallbackBehavior - The instance deciding to how the callback should be invoked.public GivenAggregateEventPublisher givenAggregate(String aggregateIdentifier)
FixtureConfigurationandThenAggregate(someIdentifier).published(someEvents)givenAggregate in interface FixtureConfigurationaggregateIdentifier - The identifier of the aggregate the events should appear to come frompublic ContinuedGivenState givenAPublished(Object event)
FixtureConfigurationapplicationEvent has been published in the past. This event is sent to the
associated sagas.givenAPublished in interface FixtureConfigurationevent - The event to publishpublic ContinuedGivenState givenCurrentTime(Instant currentTime)
FixtureConfigurationgivenCurrentTime in interface FixtureConfigurationcurrentTime - The simulated "current time" at which the given state is initializedpublic WhenState givenNoPriorActivity()
FixtureConfigurationgivenNoPriorActivity in interface FixtureConfigurationpublic GivenAggregateEventPublisher andThenAggregate(String aggregateIdentifier)
ContinuedGivenStateandThenAggregate(someIdentifier).published(someEvents)
andThenAggregate in interface ContinuedGivenStateaggregateIdentifier - The identifier of the aggregate the events should appear to come frompublic ContinuedGivenState andThenTimeElapses(Duration elapsedTime)
ContinuedGivenStateandThenTimeElapses in interface ContinuedGivenStateelapsedTime - The amount of time that will elapsepublic ContinuedGivenState andThenTimeAdvancesTo(Instant newDateTime)
ContinuedGivenStateandThenTimeAdvancesTo in interface ContinuedGivenStatenewDateTime - The time to advance the clock topublic ContinuedGivenState andThenAPublished(Object event)
ContinuedGivenStateevent has been published in the past. This event is sent to the associated
sagas.andThenAPublished in interface ContinuedGivenStateevent - The event to publishpublic ContinuedGivenState andThenAPublished(Object event, Map<String,?> metaData)
ContinuedGivenStateevent with given metaData has been published in the past. This event is sent to the associated
sagas.andThenAPublished in interface ContinuedGivenStateevent - The event to publishmetaData - The meta data to attach to the eventpublic WhenAggregateEventPublisher whenAggregate(String aggregateIdentifier)
WhenStateFixtureConfiguration.givenAggregate(String) given} and
ContinuedGivenState.andThenAggregate(String) andThen} methods, this method
will start recording activity on the EventBus and CommandBus.
Can be chained to build natural sentences:whenAggregate(someIdentifier).publishes(anEvent)
Note that if you inject resources using FixtureConfiguration.registerResource(Object), you may need to
reset them yourself if they are manipulated by the Saga in the "given" stage of the test.whenAggregate in interface WhenStateaggregateIdentifier - The identifier of the aggregate the events should appear to come frompublic FixtureExecutionResult whenPublishingA(Object event)
WhenStateFixtureConfiguration.registerResource(Object), you may need to
reset them yourself if they are manipulated by the Saga in the "given" stage of the test.whenPublishingA in interface WhenStateevent - the event to publishpublic FixtureExecutionResult whenPublishingA(Object event, Map<String,?> metaData)
WhenStatemetaData,
while recording the outcome.
Note that if you inject resources using FixtureConfiguration.registerResource(Object), you may need to
reset them yourself if they are manipulated by the Saga in the "given" stage of the test.whenPublishingA in interface WhenStateevent - the event to publishmetaData - The meta data to attach to the eventpublic Instant currentTime()
FixtureConfigurationcurrentTime in interface FixtureConfigurationpublic <I> I registerCommandGateway(Class<I> gatewayInterface)
FixtureConfigurationgatewayInterface and registers that as a resource. The gateway
will dispatch commands on the Command Bus contained in this Fixture, so that you can validate commands using
FixtureExecutionResult.expectDispatchedCommands(Object...) and FixtureExecutionResult.expectDispatchedCommandsMatching(org.hamcrest.Matcher).
Note that you need to use FixtureConfiguration.setCallbackBehavior(org.axonframework.test.utils.CallbackBehavior) to defined
the behavior of commands when expecting return values. Alternatively, you can use FixtureConfiguration.registerCommandGateway(Class, Object) to define behavior using a stub implementation.registerCommandGateway in interface FixtureConfigurationI - The gateway typegatewayInterface - The interface describing the gatewaypublic <I> I registerCommandGateway(Class<I> gatewayInterface, I stubImplementation)
FixtureConfigurationgatewayInterface and registers that as a resource. The gateway
will dispatch commands on the Command Bus contained in this Fixture, so that you can validate commands using
FixtureExecutionResult.expectDispatchedCommands(Object...) and FixtureExecutionResult.expectDispatchedCommandsMatching(org.hamcrest.Matcher).
The behavior of the created gateway is defined by the given stubImplementation, if not null. Dispatched
Commands are still recorded for verification. Note that only commands executed in the "when" phase are recorded,
while the stub implementation may record activity during the "given" phase as well.registerCommandGateway in interface FixtureConfigurationI - The gateway typegatewayInterface - The interface describing the gatewaystubImplementation - The stub or mock implementation defining behavior of the gatewaypublic FixtureConfiguration registerFieldFilter(FieldFilter fieldFilter)
FixtureConfigurationfieldFilter, which is used to define which Fields are used when comparing objects.
The ResultValidator.expectEvents(Object...) and ResultValidator.expectResultMessage(CommandResultMessage),
for example, use this filter.
When multiple filters are registered, a Field must be accepted by all registered filters in order to be
accepted.
By default, all Fields are included in the comparison.registerFieldFilter in interface FixtureConfigurationfieldFilter - The FieldFilter that defines which fields to include in the comparisonpublic FixtureConfiguration registerIgnoredField(Class<?> declaringClass, String fieldName)
FixtureConfigurationfieldName, which is declared in given declaringClass is ignored
when performing deep equality checks.registerIgnoredField in interface FixtureConfigurationdeclaringClass - The class declaring the fieldfieldName - The name of the fieldpublic FixtureConfiguration registerHandlerDefinition(HandlerDefinition handlerDefinition)
FixtureConfigurationHandlerDefinition within this fixture. The given handlerDefinition is added to the
handler definitions introduced through ClasspathHandlerDefinition.forClass(Class).
The type of the saga under test is used as input for the ClasspathHandlerDefinition#forClass(Class)
operation.registerHandlerDefinition in interface FixtureConfigurationhandlerDefinition - used to create concrete handlerspublic FixtureConfiguration registerHandlerEnhancerDefinition(HandlerEnhancerDefinition handlerEnhancerDefinition)
FixtureConfigurationHandlerEnhancerDefinition within this fixture. This given handlerEnhancerDefinition
is added to the handler enhancer definitions introduced through ClasspathHandlerEnhancerDefinition.forClass(Class).
The type of the saga under test is used as input for the ClasspathHandlerEnhancerDefinition#forClass(Class)
operation.registerHandlerEnhancerDefinition in interface FixtureConfigurationhandlerEnhancerDefinition - the HandlerEnhancerDefinition to register within this fixturepublic FixtureConfiguration registerDeadlineDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage<?>> deadlineDispatchInterceptor)
FixtureConfigurationDeadlineManager to perform a task specified in the
interceptor.registerDeadlineDispatchInterceptor in interface FixtureConfigurationdeadlineDispatchInterceptor - the interceptor for dispatching (scheduling) deadlinespublic FixtureConfiguration registerDeadlineHandlerInterceptor(MessageHandlerInterceptor<? super DeadlineMessage<?>> deadlineHandlerInterceptor)
FixtureConfigurationregisterDeadlineHandlerInterceptor in interface FixtureConfigurationdeadlineHandlerInterceptor - the interceptor for handling deadlinespublic FixtureConfiguration registerEventHandlerInterceptor(MessageHandlerInterceptor<? super EventMessage<?>> eventHandlerInterceptor)
FixtureConfigurationMessageHandlerInterceptor for EventMessages.
Will always be invoked before an event is handled to perform a task specified in the interceptor. Interceptors are invoked in the order they have been registered in.
registerEventHandlerInterceptor in interface FixtureConfigurationeventHandlerInterceptor - the interceptor for handling EventMessagesFixtureConfiguration, for fluent interfacing.public FixtureConfiguration registerStartRecordingCallback(Runnable onStartRecordingCallback)
FixtureConfigurationregisterStartRecordingCallback in interface FixtureConfigurationonStartRecordingCallback - callback to invokepublic FixtureConfiguration registerListenerInvocationErrorHandler(ListenerInvocationErrorHandler listenerInvocationErrorHandler)
FixtureConfigurationListenerInvocationErrorHandler to be set for the Saga to deal with exceptions being thrown from within Saga Event Handlers. Will be
given to the AnnotatedSagaManager for the defined Saga type. Defaults to a LoggingErrorHandler wrapped inside a RecordingListenerInvocationErrorHandler.registerListenerInvocationErrorHandler in interface FixtureConfigurationlistenerInvocationErrorHandler - to be set for the Saga to deal with exceptions being thrown from within Saga Event Handlerspublic FixtureConfiguration suppressExceptionInGivenPhase(boolean suppress)
FixtureConfigurationsuppress is
true, the fixture moves on to the when-phase regardless of any exceptions thrown during the given-phase.
Note that setting this to true means the
registered
ListenerInvocationErrorHandler is not invoked during exception in the given-phase. Defaults to
suppressing during given-phase exceptions.
suppressExceptionInGivenPhase in interface FixtureConfigurationsuppress - A boolean describing whether the fixture should suppress failures during the
given-phase.public FixtureConfiguration registerResourceInjector(ResourceInjector resourceInjector)
FixtureConfigurationResourceInjector within this fixture. This approach can be used if a custom ResourceInjector has been built for a project which the user wants to take into account when testing it's
sagas.
The provided resourceInjector will be paired with the fixture's default ResourceInjector to keep
support for the FixtureConfiguration.registerResource(Object) and FixtureConfiguration.withTransienceCheckDisabled() methods. Note that
first the default injector is called, and after that the given resourceInjector. This approach
ensures the fixture's correct workings for default provided resources, like the EventBus and CommandBus}, whilst allowing the capability to append and/or override with the given resourceInjector.
Care should be taken if the custom resourceInjector overrides default resources like the EventBus
and CommandBus, as the fixture uses specialized versions of the default sources to support all testing
functionality.
registerResourceInjector in interface FixtureConfigurationresourceInjector - the ResourceInjector to register within this fixturepublic EventBus getEventBus()
FixtureConfigurationResultValidator operations.getEventBus in interface FixtureConfigurationpublic RecordingCommandBus getCommandBus()
FixtureConfigurationgetCommandBus in interface FixtureConfigurationCopyright © 2010–2023. All rights reserved.