Interface FixtureConfiguration<T>
- Type Parameters:
T- The type of the aggregate under test
- All Known Implementing Classes:
AggregateTestFixture
@CommandHandler aggregate. If you
have implemented your own command handler (either using annotations, or by implementing the MessageHandler
interface), you must register the command handler using registerAnnotatedCommandHandler(Object) or registerCommandHandler(Class, MessageHandler), respectively. A typical command
handler will require a repository. The test fixture initializes an Event Sourcing Repository, which can be obtained
using getRepository(). Alternatively, you can register your own repository using the registerRepository(Repository) method. Registering the repository
will cause the fixture to configure the correct EventBus and EventStore implementations required by
the test.
Typical usage example:
public class MyCommandHandlerTest() {
private FixtureConfiguration fixture;
@Before
public void setUp() {
fixture = new AggregateTestFixture(MyAggregate.class);
MyCommandHandler commandHandler = new MyCommandHandler();
commandHandler.setRepository(fixture.getRepository());
fixture.registerAnnotatedCommandHandler(commandHandler);
}
@Test
public void testCommandHandlerCase() {
fixture.given(new MyEvent(1), new MyEvent(2))
.when(new TestCommand())
.expectResultMessagePayload(null)
.expectEvents(new MyEvent(3));
}
}
If you use @CommandHandler annotations on the aggregate, you do not need to configure any additional command
handlers. In that case, no configuration is required:
Providing the "given" events using the given(Object...) or given(List<DomainEvent>) methods must be the last operation in the configuration
stage. To indicate that no "given" events are available, just call given() with no parameters.
Besides setting configuration, you can also use the FixtureConfiguration to get access to the configured components.
This allows you to (manually) inject the EventBus or any other component into you command handler, for example.- Since:
- 0.6
- Author:
- Allard Buijze
-
Method Summary
Modifier and TypeMethodDescriptionReturns the command bus used by this fixture.Returns the event bus used by this fixture.Returns the event store used by this fixture.Returns the repository used by this fixture.Configures the givendomainEventsas the "given" events.Configures the givendomainEventsas the "given" events.givenCommands(Object... commands) Configures the givencommandsas to execute against the aggregate under test to initiate the given-phase.givenCommands(List<?> commands) Configures the givencommandsas to execute against the aggregate under test to initiate the given-phase.givenCurrentTime(Instant time) 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 activities like commands or events have occurred in the past.givenState(Supplier<T> aggregateState) Sets the aggregate instance as supplied by givenaggregateStateas the initial state for a test case.registerAggregateFactory(AggregateFactory<T> aggregateFactory) Registers the givenaggregateFactorywith the fixture.registerAnnotatedCommandHandler(Object annotatedCommandHandler) Registers anannotatedCommandHandlerwith this fixture.registerCommandDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> commandDispatchInterceptor) Register aMessageDispatchInterceptorforCommandMessages which will be invoked before any command is dispatched on theCommandBusto perform a task specified in the interceptor.registerCommandHandler(Class<?> payloadType, MessageHandler<CommandMessage<?>> commandHandler) Registers acommandHandlerto handle commands of the givencommandTypewith the command bus used by this fixture.registerCommandHandler(String commandName, MessageHandler<CommandMessage<?>> commandHandler) Registers acommandHandlerto handle commands of the givencommandTypewith the command bus used by this fixture.registerCommandHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> commandHandlerInterceptor) Register aMessageHandlerInterceptorforCommandMessages which will be invoked before or after the command has been dispatched on theCommandBusto perform a task specified in the interceptor.registerCommandTargetResolver(CommandTargetResolver commandTargetResolver) Registers theCommandTargetResolverwithin this fixture.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.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.registerInjectableResource(Object resource) Registers a resource that is eligible for injection in handler method (e.g. methods annotated with@CommandHandler,@EventSourcingHandlerandEventHandler.default FixtureConfiguration<T> registerInjectableResources(Object... resources) Default implementation to register multiple resources in handler method (e.g. methods annotated with@CommandHandler,@EventSourcingHandlerandEventHandler.registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory) Registers aParameterResolverFactorywithin this fixture.registerRepository(Repository<T> repository) Registers an arbitraryrepositorywith the fixture.registerRepositoryProvider(RepositoryProvider repositoryProvider) Registers repository provider with the fixture.voidsetReportIllegalStateChange(boolean reportIllegalStateChange) Sets whether or not the fixture should detect and report state changes that occur outside of Event Handler methods.Configures the fixture for state stored aggregates.withSubtypes(Class<? extends T>... subtypes) Registers subtypes of this aggregate to support aggregate polymorphism.
-
Method Details
-
withSubtypes
Registers subtypes of this aggregate to support aggregate polymorphism. Command Handlers defined on this subtype will be considered part of this aggregate's handlers.- Parameters:
subtypes- subtypes in this polymorphic hierarchy- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
useStateStorage
FixtureConfiguration<T> useStateStorage()Configures the fixture for state stored aggregates. This will register an in-memorywith this fixture. Should be used before callinginvalid reference
org.axonframework.commandhandling.model.RepositorygivenState(Supplier)orgivenCommands(List)(Supplier)}.- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerRepository
Registers an arbitraryrepositorywith the fixture. The repository must be wired with the Event Store of this test fixture. Should not be used in combination withregisterAggregateFactory(org.axonframework.eventsourcing.AggregateFactory), as that will overwrite any repository previously registered.- Parameters:
repository- The repository to use in the test case- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerRepositoryProvider
Registers repository provider with the fixture. If an aggregate being tested spawns new aggregates, this provider should be registered. Otherwise, it is not going to be invoked.- Parameters:
repositoryProvider- provides repositories for specified aggregate types- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerAggregateFactory
Registers the givenaggregateFactorywith the fixture. The repository used by the fixture will use the given factory to create new aggregate instances. Defaults to an Aggregate Factory that uses the no-arg constructor to create new instances. Should not be used in combination withregisterRepository(Repository), as that will overwrite any aggregate factory previously registered.- Parameters:
aggregateFactory- The Aggregate Factory to create empty aggregates with- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerAnnotatedCommandHandler
Registers anannotatedCommandHandlerwith this fixture. This will register this command handler with the command bus used in this fixture.- Parameters:
annotatedCommandHandler- The command handler to register for this test- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerCommandHandler
FixtureConfiguration<T> registerCommandHandler(Class<?> payloadType, MessageHandler<CommandMessage<?>> commandHandler) Registers acommandHandlerto handle commands of the givencommandTypewith the command bus used by this fixture.- Parameters:
payloadType- The type of command to register the handler forcommandHandler- The handler to register- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerCommandHandler
FixtureConfiguration<T> registerCommandHandler(String commandName, MessageHandler<CommandMessage<?>> commandHandler) Registers acommandHandlerto handle commands of the givencommandTypewith the command bus used by this fixture.- Parameters:
commandName- The name of the command to register the handler forcommandHandler- The handler to register- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerInjectableResource
Registers a resource that is eligible for injection in handler method (e.g. methods annotated with@CommandHandler,@EventSourcingHandlerandEventHandler. These resource must be registered before registering any command handler.- Parameters:
resource- the resource eligible for injection- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerInjectableResources
Default implementation to register multiple resources in handler method (e.g. methods annotated with@CommandHandler,@EventSourcingHandlerandEventHandler. These resource must be registered before registering any command handler. Internally this method callsinvalid input: 'for each resource. @param resources collection of resources eligible for injection @return the current FixtureConfiguration, for fluent interfacing' -
registerParameterResolverFactory
FixtureConfiguration<T> registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory) Registers aParameterResolverFactorywithin this fixture. The givenparameterResolverFactorywill be added to the other parameter resolver factories introduced throughClasspathParameterResolverFactory.forClass(Class)and theSimpleResourceParameterResolverFactoryadding the registered resources (withregisterInjectableResource(Object). The genericTis used as input for theClasspathParameterResolverFactory#forClass(Class)operation.- Parameters:
parameterResolverFactory- theParameterResolverto register within this fixture- Returns:
- the current FixtureConfiguration, for fluent interfacing
- See Also:
-
registerCommandDispatchInterceptor
FixtureConfiguration<T> registerCommandDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> commandDispatchInterceptor) Register aMessageDispatchInterceptorforCommandMessages which will be invoked before any command is dispatched on theCommandBusto perform a task specified in the interceptor. For example by addingMetaDataor throwing an exception based on the command.- Parameters:
commandDispatchInterceptor- theMessageDispatchInterceptorforCommandMessages to be added to this fixture'sCommandBus- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerCommandHandlerInterceptor
FixtureConfiguration<T> registerCommandHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> commandHandlerInterceptor) Register aMessageHandlerInterceptorforCommandMessages which will be invoked before or after the command has been dispatched on theCommandBusto perform a task specified in the interceptor. It could for example block the command for security reasons or add auditing to the command bus- Parameters:
commandHandlerInterceptor- theMessageHandlerInterceptorforCommandMessages to be added to this fixture'sCommandBus- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerDeadlineDispatchInterceptor
FixtureConfiguration<T> 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.- Parameters:
deadlineDispatchInterceptor- the interceptor for dispatching (scheduling) deadlines- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerDeadlineHandlerInterceptor
FixtureConfiguration<T> 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.- Parameters:
deadlineHandlerInterceptor- the interceptor for handling deadlines- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerFieldFilter
Registers the givenfieldFilter, which is used to define which Fields are used when comparing objects. TheResultValidator.expectEvents(Object...)andResultValidator.expectResultMessagePayload(Object), 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.- Parameters:
fieldFilter- The FieldFilter that defines which fields to include in the comparison- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerIgnoredField
Indicates that a field with givenfieldName, which is declared in givendeclaringClassis ignored when performing deep equality checks.- Parameters:
declaringClass- The class declaring the fieldfieldName- The name of the field- Returns:
- the current FixtureConfiguration, for fluent interfacing
- Throws:
FixtureExecutionException- when no such field is declared
-
registerHandlerDefinition
Registers aHandlerDefinitionwithin this fixture. The givenhandlerDefinitionis added to the handler definitions introduced throughClasspathHandlerDefinition.forClass(Class). The genericTis used as input for theClasspathHandlerDefinition#forClass(Class)operation.- Parameters:
handlerDefinition- used to create concrete handlers- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerHandlerEnhancerDefinition
FixtureConfiguration<T> registerHandlerEnhancerDefinition(HandlerEnhancerDefinition handlerEnhancerDefinition) Registers aHandlerEnhancerDefinitionwithin this fixture. This givenhandlerEnhancerDefinitionis added to the handler enhancer definitions introduced throughClasspathHandlerEnhancerDefinition.forClass(Class). The genericTis used as input for theClasspathHandlerEnhancerDefinition#forClass(Class)operation.- Parameters:
handlerEnhancerDefinition- theHandlerEnhancerDefinitionto register within this fixture- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
registerCommandTargetResolver
Registers theCommandTargetResolverwithin this fixture. ThecommandTargetResolverwill replace the default implementation (defined by theAggregateAnnotationCommandHandlerwithin this fixture.- Parameters:
commandTargetResolver- theCommandTargetResolverused to resolve an Aggregate for a given command- Returns:
- the current FixtureConfiguration, for fluent interfacing
-
given
Configures the givendomainEventsas the "given" events. These are the events returned by the event store when an aggregate is loaded. If an item in the givendomainEventsimplementsMessage, the payload andMetaDatafrom thatMessageare copied into a newly createdDomainEventMessage. Otherwise, aDomainEventMessagewith the item as payload and emptyMetaDatais created.Note that transitioning to the returned
TestExecutorwill clear any previously defined "given" state to ensure the fixture can run a clean test environment.- Parameters:
domainEvents- The domain events the event store should return.- Returns:
- A
TestExecutorinstance that can execute the test with this configuration.
-
givenState
Sets the aggregate instance as supplied by givenaggregateStateas the initial state for a test case.Usage of this method is highly discouraged for event sourced aggregates. In that case, use
given(Object...)to specify historic events.Note that transitioning to the returned
TestExecutorwill clear any previously defined "given" state to ensure the fixture can run a clean test environment.- Parameters:
aggregateState- ASupplierproviding the state to use as starting point for this fixture.- Returns:
- A
TestExecutorinstance that can execute the test with this configuration.
-
givenNoPriorActivity
TestExecutor<T> givenNoPriorActivity()Indicates that no relevant activities like commands or events have occurred in the past. This also means that no previous state is present in the repository.- Returns:
- A
TestExecutorinstance that can execute the test with this configuration.
-
given
Configures the givendomainEventsas the "given" events. These are the events returned by the event store when an aggregate is loaded. If an item in the list implementsMessage, the payload andMetaDatafrom thatMessageare copied into a newly createdDomainEventMessage. Otherwise, aDomainEventMessagewith the item as payload and emptyMetaDatais created.Note that transitioning to the returned
TestExecutorwill clear any previously defined "given" state to ensure the fixture can run a clean test environment.- Parameters:
domainEvents- The domain events the event store should return.- Returns:
- A
TestExecutorinstance that can execute the test with this configuration.
-
givenCommands
Configures the givencommandsas to execute against the aggregate under test to initiate the given-phase. The commands are executed, and the resulting stored events are captured.Note that transitioning to the returned
TestExecutorwill clear any previously defined "given" state to ensure the fixture can run a clean test environment.- Parameters:
commands- The commands to execute against the aggregate under test to initiate the given-phase.- Returns:
- A
TestExecutorinstance that can execute the test with this configuration.
-
givenCommands
Configures the givencommandsas to execute against the aggregate under test to initiate the given-phase. The commands are executed, and the resulting stored events are captured.Note that transitioning to the returned
TestExecutorwill clear any previously defined "given" state to ensure the fixture can run a clean test environment.- Parameters:
commands- The commands to execute against the aggregate under test to initiate the given-phase.- Returns:
- A
TestExecutorinstance that can execute the test with this configuration.
-
getCommandBus
CommandBus getCommandBus()Returns 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).- Returns:
- the command bus used by this fixture
-
getEventBus
EventBus getEventBus()Returns 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.- Returns:
- the event bus used by this fixture
-
getEventStore
EventStore getEventStore()Returns the event store used by this fixture. This event store is provided for wiring purposes only.- Returns:
- the event store used by this fixture
-
getRepository
Repository<T> getRepository()Returns the repository used by this fixture. This repository is provided for wiring purposes only. The repository is configured to use the fixture's event store to load events.- Returns:
- the repository used by this fixture
-
givenCurrentTime
Use this method to indicate a specific moment as the initial current time "known" by the fixture at the start of the given state.Note that transitioning to the returned
TestExecutorwill clear any previously defined "given" state to ensure the fixture can run a clean test environment.- Parameters:
time- AnInstantdefining the simulated "current time" at which the given state is initialized.- Returns:
- A
TestExecutorinstance that can execute the test with this configuration.
-
setReportIllegalStateChange
void setReportIllegalStateChange(boolean reportIllegalStateChange) Sets whether or not the fixture should detect and report state changes that occur outside of Event Handler methods.- Parameters:
reportIllegalStateChange- whether or not to detect and report state changes outside of Event Handler methods.
-