Class SagaTestFixture<T>
- All Implemented Interfaces:
ContinuedGivenState,FixtureConfiguration,WhenState
- Since:
- 1.1
- Author:
- Allard Buijze
-
Constructor Summary
ConstructorsConstructorDescriptionSagaTestFixture(Class<T> sagaType) Creates an instance of the AnnotatedSagaTestFixture to test sagas of the givensagaType. -
Method Summary
Modifier and TypeMethodDescriptionandThenAggregate(String aggregateIdentifier) Use this method to indicate that an aggregate with given identifier published certain events.andThenAPublished(Object event) Indicates that the giveneventhas been published in the past.andThenAPublished(Object event, Map<String, ?> metaData) Indicates that the giveneventwith givenmetaDatahas been published in the past.andThenTimeAdvancesTo(Instant newDateTime) Simulate time shifts in the current given state.andThenTimeElapses(Duration elapsedTime) Simulate time shifts in the current given state.Returns the time as "known" by the fixture.protected voidInitializes the saga resources if it hasn't already done so.Returns the command bus used by this fixture.Returns the event bus used by this fixture.givenAggregate(String aggregateIdentifier) Use this method to indicate that an aggregate with given identifier published certain events.givenAPublished(Object event) Indicates that the givenapplicationEventhas been published in the past.givenAPublished(Object event, Map<String, ?> metaData) Indicates that the giveneventwith givenmetaDatahas been published in the past.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.Indicates that no relevant activity has occurred in the past.protected voidhandleDeadline(ScopeDescriptor sagaDescriptor, DeadlineMessage<?> deadlineMessage) Handles the givendeadlineMessagein the saga described by the givensagaDescriptor.protected voidhandleInSaga(EventMessage<?> event) Handles the giveneventin the scope of a Unit of Work.<I> IregisterCommandGateway(Class<I> gatewayInterface) Creates a Command Gateway for the givengatewayInterfaceand registers that as a resource.<I> IregisterCommandGateway(Class<I> gatewayInterface, I stubImplementation) Creates a Command Gateway for the givengatewayInterfaceand registers that as a resource.registerDeadlineDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage<?>> deadlineDispatchInterceptor) Registers a deadline dispatch interceptor which will always be invoked before a deadline is dispatched (scheduled) on theDeadlineManagerto perform a task specified in the interceptor.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.registerEventHandlerInterceptor(MessageHandlerInterceptor<? super EventMessage<?>> eventHandlerInterceptor) Registers aMessageHandlerInterceptorforEventMessages.registerFieldFilter(FieldFilter fieldFilter) Registers the givenfieldFilter, which is used to define which Fields are used when comparing objects.registerHandlerDefinition(HandlerDefinition handlerDefinition) Registers aHandlerDefinitionwithin this fixture.registerHandlerEnhancerDefinition(HandlerEnhancerDefinition handlerEnhancerDefinition) Registers aHandlerEnhancerDefinitionwithin this fixture.registerIgnoredField(Class<?> declaringClass, String fieldName) Indicates that a field with givenfieldName, which is declared in givendeclaringClassis ignored when performing deep equality checks.registerListenerInvocationErrorHandler(ListenerInvocationErrorHandler listenerInvocationErrorHandler) Registers aListenerInvocationErrorHandlerto be set for the Saga to deal with exceptions being thrown from within Saga Event Handlers.registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory) Registers aParameterResolverFactorywithin this fixture.voidregisterResource(Object resource) Registers the givenresource.registerResourceInjector(ResourceInjector resourceInjector) Registers aResourceInjectorwithin this fixture.registerStartRecordingCallback(Runnable onStartRecordingCallback) Registers a callback to be invoked when the fixture execution starts recording.voidsetCallbackBehavior(CallbackBehavior callbackBehavior) Sets the instance that defines the behavior of the Command Bus when a command is dispatched with a callback.suppressExceptionInGivenPhase(boolean suppress) Configure whether the fixture should suppress exceptions thrown during the given-phase.whenAggregate(String aggregateIdentifier) Use this method to indicate that an aggregate with given identifier should publish certain events, while recording the outcome.whenPublishingA(Object event) Use this method to indicate an application is published, while recording the outcome.whenPublishingA(Object event, Map<String, ?> metaData) Use this method to indicate an application is published with given additionalmetaData, while recording the outcome.whenTimeAdvancesTo(Instant newDateTime) Mimic an elapsed time with no relevant activity for the Saga.whenTimeElapses(Duration elapsedTime) Mimic an elapsed time with no relevant activity for the Saga.Disables the check that injected resources are stored in fields that are marked 'transient'.
-
Constructor Details
-
SagaTestFixture
Creates an instance of the AnnotatedSagaTestFixture to test sagas of the givensagaType.- Parameters:
sagaType- The type of saga under test
-
-
Method Details
-
handleInSaga
Handles the giveneventin the scope of a Unit of Work. If handling the event results in an exception the exception will be wrapped in aFixtureExecutionException.- Parameters:
event- The event message to handle
-
handleDeadline
protected void handleDeadline(ScopeDescriptor sagaDescriptor, DeadlineMessage<?> deadlineMessage) throws Exception Handles the givendeadlineMessagein the saga described by the givensagaDescriptor. Deadline message is handled in the scope of aUnitOfWork. If handling the deadline results in an exception, the exception will be wrapped in aFixtureExecutionException.- Parameters:
sagaDescriptor- AScopeDescriptordescribing the saga under testdeadlineMessage- TheDeadlineMessageto be handled- Throws:
Exception
-
ensureSagaResourcesInitialized
protected void ensureSagaResourcesInitialized()Initializes the saga resources if it hasn't already done so. If once initialized, this method does nothing. -
withTransienceCheckDisabled
Description copied from interface:FixtureConfigurationDisables the check that injected resources are stored in fields that are marked 'transient'.By 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.
- Specified by:
withTransienceCheckDisabledin interfaceFixtureConfiguration- Returns:
- this instance for fluent interfacing.
-
whenTimeElapses
Description copied from interface:WhenStateMimic an elapsed time with no relevant activity for the Saga. If any Events are scheduled to be published within this time frame, they are published. All activity by the Saga on the CommandBus and EventBus (meaning that scheduled events are excluded) is recorded. Note that if you inject resources usingFixtureConfiguration.registerResource(Object), you may need to reset them yourself if they are manipulated by the Saga in the "given" stage of the test.- Specified by:
whenTimeElapsesin interfaceWhenState- Parameters:
elapsedTime- The amount of time to elapse- Returns:
- an object allowing you to verify the test results
-
whenTimeAdvancesTo
Description copied from interface:WhenStateMimic an elapsed time with no relevant activity for the Saga. If any Events are scheduled to be published within this time frame, they are published. All activity by the Saga on the CommandBus and EventBus (meaning that scheduled events are excluded) is recorded. Note that if you inject resources usingFixtureConfiguration.registerResource(Object), you may need to reset them yourself if they are manipulated by the Saga in the "given" stage of the test.- Specified by:
whenTimeAdvancesToin interfaceWhenState- Parameters:
newDateTime- The time to advance the clock to- Returns:
- an object allowing you to verify the test results
-
registerResource
Description copied from interface:FixtureConfigurationRegisters the givenresource. 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.- Specified by:
registerResourcein interfaceFixtureConfiguration- Parameters:
resource- the resource to register.
-
registerParameterResolverFactory
public FixtureConfiguration registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory) Description copied from interface:FixtureConfigurationRegisters aParameterResolverFactorywithin this fixture. The givenparameterResolverFactorywill be added to the other parameter resolver factories introduced throughClasspathParameterResolverFactory.forClass(Class)and theSimpleResourceParameterResolverFactoryadding the registered resources (withFixtureConfiguration.registerResource(Object). The type of the saga under test is used as input for theClasspathParameterResolverFactory#forClass(Class)operation.- Specified by:
registerParameterResolverFactoryin interfaceFixtureConfiguration- Parameters:
parameterResolverFactory- theParameterResolverto register within this fixture- Returns:
- the current FixtureConfiguration, for fluent interfacing
- See Also:
-
setCallbackBehavior
Description copied from interface:FixtureConfigurationSets the instance that defines the behavior of the Command Bus when a command is dispatched with a callback.- Specified by:
setCallbackBehaviorin interfaceFixtureConfiguration- Parameters:
callbackBehavior- The instance deciding to how the callback should be invoked.
-
givenAggregate
Description copied from interface:FixtureConfigurationUse this method to indicate that an aggregate with given identifier published certain events. Can be chained to build natural sentences:
andThenAggregate(someIdentifier).published(someEvents)- Specified by:
givenAggregatein interfaceFixtureConfiguration- Parameters:
aggregateIdentifier- The identifier of the aggregate the events should appear to come from- Returns:
- an object that allows registration of the actual events to send
-
givenAPublished
Description copied from interface:FixtureConfigurationIndicates that the givenapplicationEventhas been published in the past. This event is sent to the associated sagas.- Specified by:
givenAPublishedin interfaceFixtureConfiguration- Parameters:
event- The event to publish- Returns:
- an object that allows chaining of more given state
-
givenAPublished
Description copied from interface:FixtureConfigurationIndicates that the giveneventwith givenmetaDatahas been published in the past. This event is sent to the associated sagas.- Specified by:
givenAPublishedin interfaceFixtureConfiguration- Parameters:
event- The event to publishmetaData- The meta data to attach to the event- Returns:
- an object that allows chaining of more given state
-
givenCurrentTime
Description copied from interface:FixtureConfigurationUse this method to indicate a specific moment as the initial current time "known" by the fixture at the start of the given state.- Specified by:
givenCurrentTimein interfaceFixtureConfiguration- Parameters:
currentTime- The simulated "current time" at which the given state is initialized- Returns:
- an object that allows chaining of more given state
-
givenNoPriorActivity
Description copied from interface:FixtureConfigurationIndicates that no relevant activity has occurred in the past.- Specified by:
givenNoPriorActivityin interfaceFixtureConfiguration- Returns:
- an object that allows the definition of the activity to measure Saga behavior
-
andThenAggregate
Description copied from interface:ContinuedGivenStateUse this method to indicate that an aggregate with given identifier published certain events. Can be chained to build natural sentences:
andThenAggregate(someIdentifier).published(someEvents)- Specified by:
andThenAggregatein interfaceContinuedGivenState- Parameters:
aggregateIdentifier- The identifier of the aggregate the events should appear to come from- Returns:
- an object that allows registration of the actual events to send
-
andThenTimeElapses
Description copied from interface:ContinuedGivenStateSimulate time shifts in the current given state. This can be useful when the time between given events is of importance.- Specified by:
andThenTimeElapsesin interfaceContinuedGivenState- Parameters:
elapsedTime- The amount of time that will elapse- Returns:
- an object that allows registration of the actual events to send
-
andThenTimeAdvancesTo
Description copied from interface:ContinuedGivenStateSimulate time shifts in the current given state. This can be useful when the time between given events is of importance.- Specified by:
andThenTimeAdvancesToin interfaceContinuedGivenState- Parameters:
newDateTime- The time to advance the clock to- Returns:
- an object that allows registration of the actual events to send
-
andThenAPublished
Description copied from interface:ContinuedGivenStateIndicates that the giveneventhas been published in the past. This event is sent to the associated sagas.- Specified by:
andThenAPublishedin interfaceContinuedGivenState- Parameters:
event- The event to publish- Returns:
- an object that allows chaining of more given state
-
andThenAPublished
Description copied from interface:ContinuedGivenStateIndicates that the giveneventwith givenmetaDatahas been published in the past. This event is sent to the associated sagas.- Specified by:
andThenAPublishedin interfaceContinuedGivenState- Parameters:
event- The event to publishmetaData- The meta data to attach to the event- Returns:
- an object that allows chaining of more given state
-
whenAggregate
Description copied from interface:WhenStateUse this method to indicate that an aggregate with given identifier should publish certain events, while recording the outcome. In contrast to theFixtureConfiguration.givenAggregate(String)given} andContinuedGivenState.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 usingFixtureConfiguration.registerResource(Object), you may need to reset them yourself if they are manipulated by the Saga in the "given" stage of the test.- Specified by:
whenAggregatein interfaceWhenState- Parameters:
aggregateIdentifier- The identifier of the aggregate the events should appear to come from- Returns:
- an object that allows registration of the actual events to send
-
whenPublishingA
Description copied from interface:WhenStateUse this method to indicate an application is published, while recording the outcome. Note that if you inject resources usingFixtureConfiguration.registerResource(Object), you may need to reset them yourself if they are manipulated by the Saga in the "given" stage of the test.- Specified by:
whenPublishingAin interfaceWhenState- Parameters:
event- the event to publish- Returns:
- an object allowing you to verify the test results
-
whenPublishingA
Description copied from interface:WhenStateUse this method to indicate an application is published with given additionalmetaData, while recording the outcome. Note that if you inject resources usingFixtureConfiguration.registerResource(Object), you may need to reset them yourself if they are manipulated by the Saga in the "given" stage of the test.- Specified by:
whenPublishingAin interfaceWhenState- Parameters:
event- the event to publishmetaData- The meta data to attach to the event- Returns:
- an object allowing you to verify the test results
-
currentTime
Description copied from interface:FixtureConfigurationReturns the time as "known" by the fixture. This is the time at which the fixture was created, plus the amount of time the fixture was told to simulate a "wait". This time can be used to predict calculations that the saga may have made based on timestamps from the events it received.- Specified by:
currentTimein interfaceFixtureConfiguration- Returns:
- the simulated "current time" of the fixture.
-
registerCommandGateway
Description copied from interface:FixtureConfigurationCreates a Command Gateway for the givengatewayInterfaceand registers that as a resource. The gateway will dispatch commands on the Command Bus contained in this Fixture, so that you can validate commands usingFixtureExecutionResult.expectDispatchedCommands(Object...)andFixtureExecutionResult.expectDispatchedCommandsMatching(org.hamcrest.Matcher). Note that you need to useFixtureConfiguration.setCallbackBehavior(org.axonframework.test.utils.CallbackBehavior)to defined the behavior of commands when expecting return values. Alternatively, you can useFixtureConfiguration.registerCommandGateway(Class, Object)to define behavior using a stub implementation.- Specified by:
registerCommandGatewayin interfaceFixtureConfiguration- Type Parameters:
I- The gateway type- Parameters:
gatewayInterface- The interface describing the gateway- Returns:
- the gateway implementation being registered as a resource.
-
registerCommandGateway
Description copied from interface:FixtureConfigurationCreates a Command Gateway for the givengatewayInterfaceand registers that as a resource. The gateway will dispatch commands on the Command Bus contained in this Fixture, so that you can validate commands usingFixtureExecutionResult.expectDispatchedCommands(Object...)andFixtureExecutionResult.expectDispatchedCommandsMatching(org.hamcrest.Matcher). The behavior of the created gateway is defined by the givenstubImplementation, 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.- Specified by:
registerCommandGatewayin interfaceFixtureConfiguration- Type Parameters:
I- The gateway type- Parameters:
gatewayInterface- The interface describing the gatewaystubImplementation- The stub or mock implementation defining behavior of the gateway- Returns:
- the gateway implementation being registered as a resource.
-
registerFieldFilter
Description copied from interface:FixtureConfigurationRegisters the givenfieldFilter, which is used to define which Fields are used when comparing objects. TheResultValidator.expectEvents(Object...)andResultValidator.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.- Specified by:
registerFieldFilterin interfaceFixtureConfiguration- Parameters:
fieldFilter- The FieldFilter that defines which fields to include in the comparison- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerIgnoredField
Description copied from interface:FixtureConfigurationIndicates that a field with givenfieldName, which is declared in givendeclaringClassis ignored when performing deep equality checks.- Specified by:
registerIgnoredFieldin interfaceFixtureConfiguration- Parameters:
declaringClass- The class declaring the fieldfieldName- The name of the field- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerHandlerDefinition
Description copied from interface:FixtureConfigurationRegisters aHandlerDefinitionwithin this fixture. The givenhandlerDefinitionis added to the handler definitions introduced throughClasspathHandlerDefinition.forClass(Class). The type of the saga under test is used as input for theClasspathHandlerDefinition#forClass(Class)operation.- Specified by:
registerHandlerDefinitionin interfaceFixtureConfiguration- Parameters:
handlerDefinition- used to create concrete handlers- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerHandlerEnhancerDefinition
public FixtureConfiguration registerHandlerEnhancerDefinition(HandlerEnhancerDefinition handlerEnhancerDefinition) Description copied from interface:FixtureConfigurationRegisters aHandlerEnhancerDefinitionwithin this fixture. This givenhandlerEnhancerDefinitionis added to the handler enhancer definitions introduced throughClasspathHandlerEnhancerDefinition.forClass(Class). The type of the saga under test is used as input for theClasspathHandlerEnhancerDefinition#forClass(Class)operation.- Specified by:
registerHandlerEnhancerDefinitionin interfaceFixtureConfiguration- Parameters:
handlerEnhancerDefinition- theHandlerEnhancerDefinitionto register within this fixture- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerDeadlineDispatchInterceptor
public FixtureConfiguration registerDeadlineDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage<?>> deadlineDispatchInterceptor) Description copied from interface:FixtureConfigurationRegisters a deadline dispatch interceptor which will always be invoked before a deadline is dispatched (scheduled) on theDeadlineManagerto perform a task specified in the interceptor.- Specified by:
registerDeadlineDispatchInterceptorin interfaceFixtureConfiguration- Parameters:
deadlineDispatchInterceptor- the interceptor for dispatching (scheduling) deadlines- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerDeadlineHandlerInterceptor
public FixtureConfiguration registerDeadlineHandlerInterceptor(MessageHandlerInterceptor<? super DeadlineMessage<?>> deadlineHandlerInterceptor) Description copied from interface:FixtureConfigurationRegisters a deadline handler interceptor which will always be invoked before a deadline is handled to perform a task specified in the interceptor.- Specified by:
registerDeadlineHandlerInterceptorin interfaceFixtureConfiguration- Parameters:
deadlineHandlerInterceptor- the interceptor for handling deadlines- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerEventHandlerInterceptor
public FixtureConfiguration registerEventHandlerInterceptor(MessageHandlerInterceptor<? super EventMessage<?>> eventHandlerInterceptor) Description copied from interface:FixtureConfigurationRegisters aMessageHandlerInterceptorforEventMessages.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.
- Specified by:
registerEventHandlerInterceptorin interfaceFixtureConfiguration- Parameters:
eventHandlerInterceptor- the interceptor for handlingEventMessages- Returns:
- The current
FixtureConfiguration, for fluent interfacing.
-
registerStartRecordingCallback
Description copied from interface:FixtureConfigurationRegisters a callback to be invoked when the fixture execution starts recording. This happens right before invocation of the 'when' step (stimulus) of the fixture. Use this to manage Saga dependencies which are not an Axon first class citizen, but do require monitoring of their interactions. For example, register the callback to set a mock in recording mode.- Specified by:
registerStartRecordingCallbackin interfaceFixtureConfiguration- Parameters:
onStartRecordingCallback- callback to invoke- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerListenerInvocationErrorHandler
public FixtureConfiguration registerListenerInvocationErrorHandler(ListenerInvocationErrorHandler listenerInvocationErrorHandler) Description copied from interface:FixtureConfigurationRegisters aListenerInvocationErrorHandlerto be set for the Saga to deal with exceptions being thrown from within Saga Event Handlers. Will be given to theAnnotatedSagaManagerfor the defined Saga type. Defaults to aLoggingErrorHandlerwrapped inside aRecordingListenerInvocationErrorHandler.- Specified by:
registerListenerInvocationErrorHandlerin interfaceFixtureConfiguration- Parameters:
listenerInvocationErrorHandler- to be set for the Saga to deal with exceptions being thrown from within Saga Event Handlers- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
suppressExceptionInGivenPhase
Description copied from interface:FixtureConfigurationConfigure whether the fixture should suppress exceptions thrown during the given-phase. Whensuppressistrue, the fixture moves on to the when-phase regardless of any exceptions thrown during the given-phase.Note that setting this to
truemeans theregisteredListenerInvocationErrorHandleris not invoked during exception in the given-phase. Defaults to suppressing during given-phase exceptions.- Specified by:
suppressExceptionInGivenPhasein interfaceFixtureConfiguration- Parameters:
suppress- Abooleandescribing whether the fixture should suppress failures during the given-phase.- Returns:
- The current fixture, for fluent interfacing.
-
registerResourceInjector
Description copied from interface:FixtureConfigurationRegisters aResourceInjectorwithin this fixture. This approach can be used if a customResourceInjectorhas been built for a project which the user wants to take into account when testing it's sagas.The provided
resourceInjectorwill be paired with the fixture's defaultResourceInjectorto keep support for theFixtureConfiguration.registerResource(Object)andFixtureConfiguration.withTransienceCheckDisabled()methods. Note that first the default injector is called, and after that the givenresourceInjector. This approach ensures the fixture's correct workings for default provided resources, like theEventBusandCommandBus}, whilst allowing the capability to append and/or override with the givenresourceInjector.Care should be taken if the custom
resourceInjectoroverrides default resources like theEventBusandCommandBus, as the fixture uses specialized versions of the default sources to support all testing functionality.- Specified by:
registerResourceInjectorin interfaceFixtureConfiguration- Parameters:
resourceInjector- theResourceInjectorto register within this fixture- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
getEventBus
Description copied from interface:FixtureConfigurationReturns the event bus used by this fixture. The event bus is provided for wiring purposes only, for example to allow command handlers to publish events other than Domain Events. Events published on the returned event bus are recorded an evaluated in theResultValidatoroperations.- Specified by:
getEventBusin interfaceFixtureConfiguration- Returns:
- the event bus used by this fixture
-
getCommandBus
Description copied from interface:FixtureConfigurationReturns the command bus used by this fixture. The command bus is provided for wiring purposes only, for example to support composite commands (a single command that causes the execution of one or more others).- Specified by:
getCommandBusin interfaceFixtureConfiguration- Returns:
- the command bus used by this fixture
-