T - The type of the aggregate under testpublic interface FixtureConfiguration<T>
@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.| Modifier and Type | Method and Description |
|---|---|
CommandBus |
getCommandBus()
Returns the command bus used by this fixture.
|
EventBus |
getEventBus()
Returns the event bus used by this fixture.
|
EventStore |
getEventStore()
Returns the event store used by this fixture.
|
Repository<T> |
getRepository()
Returns the repository used by this fixture.
|
TestExecutor<T> |
given(List<?> domainEvents)
Configures the given
domainEvents as the "given" events. |
TestExecutor<T> |
given(Object... domainEvents)
Configures the given
domainEvents as the "given" events. |
TestExecutor<T> |
givenCommands(List<?> commands)
Configures the given
commands as to execute against the aggregate under test to initiate the given-phase. |
TestExecutor<T> |
givenCommands(Object... commands)
Configures the given
commands as to execute against the aggregate under test to initiate the given-phase. |
TestExecutor<T> |
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.
|
TestExecutor<T> |
givenNoPriorActivity()
Indicates that no relevant activities like commands or events have occurred in the past.
|
TestExecutor<T> |
givenState(Supplier<T> aggregateState)
Sets the aggregate instance as supplied by given
aggregateState as the initial state for a test case. |
FixtureConfiguration<T> |
registerAggregateFactory(AggregateFactory<T> aggregateFactory)
Registers the given
aggregateFactory with the fixture. |
FixtureConfiguration<T> |
registerAnnotatedCommandHandler(Object annotatedCommandHandler)
Registers an
annotatedCommandHandler with this fixture. |
FixtureConfiguration<T> |
registerCommandDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> commandDispatchInterceptor)
Register a
MessageDispatchInterceptor for CommandMessages which will be invoked before any
command is dispatched on the CommandBus to perform a task specified in the interceptor. |
FixtureConfiguration<T> |
registerCommandHandler(Class<?> payloadType,
MessageHandler<CommandMessage<?>> commandHandler)
Registers a
commandHandler to handle commands of the given commandType with the
command bus used by this fixture. |
FixtureConfiguration<T> |
registerCommandHandler(String commandName,
MessageHandler<CommandMessage<?>> commandHandler)
Registers a
commandHandler to handle commands of the given commandType with the
command bus used by this fixture. |
FixtureConfiguration<T> |
registerCommandHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> commandHandlerInterceptor)
Register a
MessageHandlerInterceptor for CommandMessages which will be invoked before or after
the command has been dispatched on the CommandBus to perform a task specified in the interceptor. |
FixtureConfiguration<T> |
registerCommandTargetResolver(CommandTargetResolver commandTargetResolver)
Registers the
CommandTargetResolver within this fixture. |
FixtureConfiguration<T> |
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<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.
|
FixtureConfiguration<T> |
registerFieldFilter(FieldFilter fieldFilter)
Registers the given
fieldFilter, which is used to define which Fields are used when comparing objects. |
FixtureConfiguration<T> |
registerHandlerDefinition(HandlerDefinition handlerDefinition)
Registers a
HandlerDefinition within this fixture. |
FixtureConfiguration<T> |
registerHandlerEnhancerDefinition(HandlerEnhancerDefinition handlerEnhancerDefinition)
Registers a
HandlerEnhancerDefinition within this fixture. |
FixtureConfiguration<T> |
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<T> |
registerInjectableResource(Object resource)
Registers a resource that is eligible for injection in handler method (e.g.
|
FixtureConfiguration<T> |
registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory)
Registers a
ParameterResolverFactory within this fixture. |
FixtureConfiguration<T> |
registerRepository(Repository<T> repository)
Registers an arbitrary
repository with the fixture. |
FixtureConfiguration<T> |
registerRepositoryProvider(RepositoryProvider repositoryProvider)
Registers repository provider with the fixture.
|
void |
setReportIllegalStateChange(boolean reportIllegalStateChange)
Sets whether or not the fixture should detect and report state changes that occur outside of Event Handler
methods.
|
FixtureConfiguration<T> |
useStateStorage()
Configures the fixture for state stored aggregates.
|
FixtureConfiguration<T> |
withSubtypes(Class<? extends T>... subtypes)
Registers subtypes of this aggregate to support aggregate polymorphism.
|
FixtureConfiguration<T> withSubtypes(Class<? extends T>... subtypes)
subtypes - subtypes in this polymorphic hierarchyFixtureConfiguration<T> useStateStorage()
org.axonframework.commandhandling.model.Repository with this fixture.
Should be used before calling givenState(Supplier) or givenCommands(List) (Supplier)}.FixtureConfiguration<T> registerRepository(Repository<T> repository)
repository with the fixture. The repository must be wired
with the Event Store of this test fixture.
Should not be used in combination with registerAggregateFactory(org.axonframework.eventsourcing.AggregateFactory), as that will overwrite any
repository previously registered.repository - The repository to use in the test caseFixtureConfiguration<T> registerRepositoryProvider(RepositoryProvider repositoryProvider)
repositoryProvider - provides repositories for specified aggregate typesFixtureConfiguration<T> registerAggregateFactory(AggregateFactory<T> aggregateFactory)
aggregateFactory with 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 with registerRepository(Repository), as that will overwrite any aggregate factory previously registered.aggregateFactory - The Aggregate Factory to create empty aggregates withFixtureConfiguration<T> registerAnnotatedCommandHandler(Object annotatedCommandHandler)
annotatedCommandHandler with this fixture. This will register this command handler
with the command bus used in this fixture.annotatedCommandHandler - The command handler to register for this testFixtureConfiguration<T> registerCommandHandler(Class<?> payloadType, MessageHandler<CommandMessage<?>> commandHandler)
commandHandler to handle commands of the given commandType with the
command bus used by this fixture.payloadType - The type of command to register the handler forcommandHandler - The handler to registerFixtureConfiguration<T> registerCommandHandler(String commandName, MessageHandler<CommandMessage<?>> commandHandler)
commandHandler to handle commands of the given commandType with the
command bus used by this fixture.commandName - The name of the command to register the handler forcommandHandler - The handler to registerFixtureConfiguration<T> registerInjectableResource(Object resource)
@CommandHandler, @EventSourcingHandler and EventHandler.
These resource must be registered before registering any command handler.resource - the resource eligible for injectionFixtureConfiguration<T> registerParameterResolverFactory(ParameterResolverFactory parameterResolverFactory)
ParameterResolverFactory 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 registerInjectableResource(Object). The generic T is used as input for the ClasspathParameterResolverFactory#forClass(Class) operation.parameterResolverFactory - the ParameterResolver to register within this fixtureregisterInjectableResource(Object)FixtureConfiguration<T> registerCommandDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> commandDispatchInterceptor)
MessageDispatchInterceptor for CommandMessages which will be invoked before any
command is dispatched on the CommandBus to perform a task specified in the interceptor. For example by
adding MetaData or throwing an exception based on the command.commandDispatchInterceptor - the MessageDispatchInterceptor for CommandMessages to be added
to this fixture's CommandBusFixtureConfiguration<T> registerCommandHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> commandHandlerInterceptor)
MessageHandlerInterceptor for CommandMessages which will be invoked before or after
the command has been dispatched on the CommandBus to perform a task specified in the interceptor. It
could for example block the command for security reasons or add auditing to the command buscommandHandlerInterceptor - the MessageHandlerInterceptor for CommandMessages to be added to
this fixture's CommandBusFixtureConfiguration<T> registerDeadlineDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage<?>> deadlineDispatchInterceptor)
DeadlineManager to perform a task specified in the
interceptor.deadlineDispatchInterceptor - the interceptor for dispatching (scheduling) deadlinesFixtureConfiguration<T> registerDeadlineHandlerInterceptor(MessageHandlerInterceptor<? super DeadlineMessage<?>> deadlineHandlerInterceptor)
deadlineHandlerInterceptor - the interceptor for handling deadlinesFixtureConfiguration<T> registerFieldFilter(FieldFilter fieldFilter)
fieldFilter, which is used to define which Fields are used when comparing objects.
The ResultValidator.expectEvents(Object...) and ResultValidator.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.fieldFilter - The FieldFilter that defines which fields to include in the comparisonFixtureConfiguration<T> registerIgnoredField(Class<?> declaringClass, String fieldName)
fieldName, which is declared in given declaringClass
is ignored when performing deep equality checks.declaringClass - The class declaring the fieldfieldName - The name of the fieldFixtureExecutionException - when no such field is declaredFixtureConfiguration<T> registerHandlerDefinition(HandlerDefinition handlerDefinition)
HandlerDefinition within this fixture. The given handlerDefinition is added to the
handler definitions introduced through ClasspathHandlerDefinition.forClass(Class).
The generic T is used as input for the ClasspathHandlerDefinition#forClass(Class) operation.handlerDefinition - used to create concrete handlersFixtureConfiguration<T> registerHandlerEnhancerDefinition(HandlerEnhancerDefinition handlerEnhancerDefinition)
HandlerEnhancerDefinition within this fixture. This given handlerEnhancerDefinition
is added to the handler enhancer definitions introduced through ClasspathHandlerEnhancerDefinition.forClass(Class).
The generic T is used as input for the ClasspathHandlerEnhancerDefinition#forClass(Class)
operation.handlerEnhancerDefinition - the HandlerEnhancerDefinition to register within this fixtureFixtureConfiguration<T> registerCommandTargetResolver(CommandTargetResolver commandTargetResolver)
CommandTargetResolver within this fixture. The commandTargetResolver will replace
the default implementation (defined by the AggregateAnnotationCommandHandler
within this fixture.commandTargetResolver - the CommandTargetResolver used to resolve an Aggregate for a given commandTestExecutor<T> given(Object... domainEvents)
domainEvents as the "given" events. These are the events returned by the event store
when an aggregate is loaded.
If an item in the given domainEvents implements Message, the payload and MetaData from
that Message are copied into a newly created DomainEventMessage.
Otherwise, a DomainEventMessage with the item as payload and empty MetaData is created.
Note that transitioning to the returned TestExecutor will clear any previously defined "given" state to
ensure the fixture can run a clean test environment.
domainEvents - The domain events the event store should return.TestExecutor instance that can execute the test with this configuration.TestExecutor<T> givenState(Supplier<T> aggregateState)
aggregateState as 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 TestExecutor will clear any previously defined "given" state to
ensure the fixture can run a clean test environment.
aggregateState - A Supplier providing the state to use as starting point for this fixture.TestExecutor instance that can execute the test with this configuration.TestExecutor<T> givenNoPriorActivity()
TestExecutor instance that can execute the test with this configuration.TestExecutor<T> given(List<?> domainEvents)
domainEvents as the "given" events. These are the events returned by the event store
when an aggregate is loaded.
If an item in the list implements Message, the payload and MetaData from that Message are
copied into a newly created DomainEventMessage. Otherwise, a
DomainEventMessage with the item as payload and empty MetaData is created.
Note that transitioning to the returned TestExecutor will clear any previously defined "given" state to
ensure the fixture can run a clean test environment.
domainEvents - The domain events the event store should return.TestExecutor instance that can execute the test with this configuration.TestExecutor<T> givenCommands(Object... commands)
commands as 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 TestExecutor will clear any previously defined "given" state to
ensure the fixture can run a clean test environment.
commands - The commands to execute against the aggregate under test to initiate the given-phase.TestExecutor instance that can execute the test with this configuration.TestExecutor<T> givenCommands(List<?> commands)
commands as 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 TestExecutor will clear any previously defined "given" state to
ensure the fixture can run a clean test environment.
commands - The commands to execute against the aggregate under test to initiate the given-phase.TestExecutor instance that can execute the test with this configuration.CommandBus getCommandBus()
EventBus getEventBus()
ResultValidator operations.EventStore getEventStore()
Repository<T> getRepository()
TestExecutor<T> givenCurrentTime(Instant time)
Note that transitioning to the returned TestExecutor will clear any previously defined "given" state to
ensure the fixture can run a clean test environment.
time - An Instant defining the simulated "current time" at which the given state is initialized.TestExecutor instance that can execute the test with this configuration.void setReportIllegalStateChange(boolean reportIllegalStateChange)
reportIllegalStateChange - whether or not to detect and report state changes outside of Event Handler
methods.Copyright © 2010–2023. All rights reserved.