org.axonframework.test
Class GivenWhenThenTestFixture<T extends EventSourcedAggregateRoot>

java.lang.Object
  extended by org.axonframework.test.GivenWhenThenTestFixture<T>
Type Parameters:
T - The type of Aggregate tested in this Fixture
All Implemented Interfaces:
FixtureConfiguration<T>, TestExecutor

public class GivenWhenThenTestFixture<T extends EventSourcedAggregateRoot>
extends Object
implements FixtureConfiguration<T>, TestExecutor

A test fixture that allows the execution of given-when-then style test cases. For detailed usage information, see FixtureConfiguration.

Since:
0.6
Author:
Allard Buijze

Constructor Summary
GivenWhenThenTestFixture(Class<T> aggregateType)
          Initializes a new given-when-then style test fixture for the given aggregateType.
 
Method Summary
 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 given(List<?> domainEvents)
          Configures the given domainEvents as the "given" events.
 TestExecutor given(Object... domainEvents)
          Configures the given domainEvents as the "given" events.
 TestExecutor givenCommands(List<?> commands)
          Configures the given commands as the command that will provide the "given" events.
 TestExecutor givenCommands(Object... commands)
          Configures the given commands as the command that will provide the "given" events.
 TestExecutor givenNoPriorActivity()
          Indicates that no relevant activity has occurred in the past.
 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> registerCommandHandler(Class<?> payloadType, CommandHandler commandHandler)
          Registers a commandHandler to handle commands of the given commandType with the command bus used by this fixture.
 FixtureConfiguration<T> registerCommandHandler(String commandName, CommandHandler commandHandler)
          Registers a commandHandler to handle commands of the given commandType with the command bus used by this fixture.
 FixtureConfiguration<T> registerFieldFilter(FieldFilter fieldFilter)
          Registers the given fieldFilter, which is used to define which Fields are used when comparing objects.
 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> registerRepository(EventSourcingRepository<T> eventSourcingRepository)
          Registers an arbitrary event sourcing repository 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.
 ResultValidator when(Object command)
          Dispatches the given command to the appropriate command handler and records all activity in the fixture for result validation.
 ResultValidator when(Object command, Map<String,?> metaData)
          Dispatches the given command and meta data to the appropriate command handler and records all activity in the fixture for result validation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GivenWhenThenTestFixture

public GivenWhenThenTestFixture(Class<T> aggregateType)
Initializes a new given-when-then style test fixture for the given aggregateType.

Parameters:
aggregateType - The aggregate to initialize the test fixture for
Method Detail

registerRepository

public FixtureConfiguration<T> registerRepository(EventSourcingRepository<T> eventSourcingRepository)
Description copied from interface: FixtureConfiguration
Registers an arbitrary event sourcing repository with the fixture. The repository must be wired with the Event Store of this test fixture.

Should not be used in combination with FixtureConfiguration.registerAggregateFactory(org.axonframework.eventsourcing.AggregateFactory), as that will overwrite any repository previously registered.

Specified by:
registerRepository in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
eventSourcingRepository - The repository to use in the test case
Returns:
the current FixtureConfiguration, for fluent interfacing

registerAggregateFactory

public FixtureConfiguration<T> registerAggregateFactory(AggregateFactory<T> aggregateFactory)
Description copied from interface: FixtureConfiguration
Registers the given 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 FixtureConfiguration.registerRepository(org.axonframework.eventsourcing.EventSourcingRepository), as that will overwrite any aggregate factory previously registered.

Specified by:
registerAggregateFactory in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
aggregateFactory - The Aggregate Factory to create empty aggregates with
Returns:
the current FixtureConfiguration, for fluent interfacing

registerAnnotatedCommandHandler

public FixtureConfiguration<T> registerAnnotatedCommandHandler(Object annotatedCommandHandler)
Description copied from interface: FixtureConfiguration
Registers an annotatedCommandHandler with this fixture. This will register this command handler with the command bus used in this fixture.

Specified by:
registerAnnotatedCommandHandler in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
annotatedCommandHandler - The command handler to register for this test
Returns:
the current FixtureConfiguration, for fluent interfacing

registerCommandHandler

public FixtureConfiguration<T> registerCommandHandler(Class<?> payloadType,
                                                      CommandHandler commandHandler)
Description copied from interface: FixtureConfiguration
Registers a commandHandler to handle commands of the given commandType with the command bus used by this fixture.

Specified by:
registerCommandHandler in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
payloadType - The type of command to register the handler for
commandHandler - The handler to register
Returns:
the current FixtureConfiguration, for fluent interfacing

registerCommandHandler

public FixtureConfiguration<T> registerCommandHandler(String commandName,
                                                      CommandHandler commandHandler)
Description copied from interface: FixtureConfiguration
Registers a commandHandler to handle commands of the given commandType with the command bus used by this fixture.

Specified by:
registerCommandHandler in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
commandName - The name of the command to register the handler for
commandHandler - The handler to register
Returns:
the current FixtureConfiguration, for fluent interfacing

registerInjectableResource

public FixtureConfiguration<T> registerInjectableResource(Object resource)
Description copied from interface: FixtureConfiguration
Registers a resource that is eligible for injection in handler method (e.g. methods annotated with @CommandHandler, @EventSourcingHandler and @EventHandler. These resource must be registered before registering any command handler.

Specified by:
registerInjectableResource in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
resource - The resource eligible for injection
Returns:
the current FixtureConfiguration, for fluent interfacing

registerFieldFilter

public FixtureConfiguration<T> registerFieldFilter(FieldFilter fieldFilter)
Description copied from interface: FixtureConfiguration
Registers the given fieldFilter, which is used to define which Fields are used when comparing objects. The ResultValidator.expectEvents(Object...) and ResultValidator.expectReturnValue(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.

Specified by:
registerFieldFilter in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
fieldFilter - The FieldFilter that defines which fields to include in the comparison
Returns:
the current FixtureConfiguration, for fluent interfacing

registerIgnoredField

public FixtureConfiguration<T> registerIgnoredField(Class<?> declaringClass,
                                                    String fieldName)
Description copied from interface: FixtureConfiguration
Indicates that a field with given fieldName, which is declared in given declaringClass is ignored when performing deep equality checks.

Specified by:
registerIgnoredField in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
declaringClass - The class declaring the field
fieldName - The name of the field
Returns:
the current FixtureConfiguration, for fluent interfacing

given

public TestExecutor given(Object... domainEvents)
Description copied from interface: FixtureConfiguration
Configures the given 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 meta data from that message are copied into a newly created Domain Event Message. Otherwise, a Domain Event Message with the item as payload and empty meta data is created.

Specified by:
given in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
domainEvents - the domain events the event store should return
Returns:
a TestExecutor instance that can execute the test with this configuration

givenNoPriorActivity

public TestExecutor givenNoPriorActivity()
Description copied from interface: FixtureConfiguration
Indicates that no relevant activity has occurred in the past. The behavior of this method is identical to giving no events in the FixtureConfiguration.given(java.util.List) method.

Specified by:
givenNoPriorActivity in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Returns:
a TestExecutor instance that can execute the test with this configuration

given

public TestExecutor given(List<?> domainEvents)
Description copied from interface: FixtureConfiguration
Configures the given 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 meta data from that message are copied into a newly created Domain Event Message. Otherwise, a Domain Event Message with the item as payload and empty meta data is created.

Specified by:
given in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
domainEvents - the domain events the event store should return
Returns:
a TestExecutor instance that can execute the test with this configuration

givenCommands

public TestExecutor givenCommands(Object... commands)
Description copied from interface: FixtureConfiguration
Configures the given commands as the command that will provide the "given" events. The commands are executed, and the resulting stored events are captured.

Specified by:
givenCommands in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
commands - the domain events the event store should return
Returns:
a TestExecutor instance that can execute the test with this configuration

givenCommands

public TestExecutor givenCommands(List<?> commands)
Description copied from interface: FixtureConfiguration
Configures the given commands as the command that will provide the "given" events. The commands are executed, and the resulting stored events are captured.

Specified by:
givenCommands in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
commands - the domain events the event store should return
Returns:
a TestExecutor instance that can execute the test with this configuration

when

public ResultValidator when(Object command)
Description copied from interface: TestExecutor
Dispatches the given command to the appropriate command handler and records all activity in the fixture for result validation. If the given command is a CommandMessage instance, it will be dispatched as-is. Any other object will cause the given command to be wrapped in a CommandMessage as its payload.

Specified by:
when in interface TestExecutor
Parameters:
command - The command to execute
Returns:
a ResultValidator that can be used to validate the resulting actions of the command execution

when

public ResultValidator when(Object command,
                            Map<String,?> metaData)
Description copied from interface: TestExecutor
Dispatches the given command and meta data to the appropriate command handler and records all activity in the fixture for result validation. If the given command is a CommandMessage instance, it will be dispatched as-is, with given additional metaData. Any other object will cause the given command to be wrapped in a CommandMessage as its payload.

Specified by:
when in interface TestExecutor
Parameters:
command - The command to execute
metaData - The meta data to attach to the
Returns:
a ResultValidator that can be used to validate the resulting actions of the command execution

setReportIllegalStateChange

public void setReportIllegalStateChange(boolean reportIllegalStateChange)
Description copied from interface: FixtureConfiguration
Sets whether or not the fixture should detect and report state changes that occur outside of Event Handler methods.

Specified by:
setReportIllegalStateChange in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Parameters:
reportIllegalStateChange - whether or not to detect and report state changes outside of Event Handler methods.

getCommandBus

public CommandBus getCommandBus()
Description copied from interface: FixtureConfiguration
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).

Specified by:
getCommandBus in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Returns:
the command bus used by this fixture

getEventBus

public EventBus getEventBus()
Description copied from interface: FixtureConfiguration
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 the ResultValidator operations.

Specified by:
getEventBus in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Returns:
the event bus used by this fixture

getEventStore

public EventStore getEventStore()
Description copied from interface: FixtureConfiguration
Returns the event store used by this fixture. This event store is provided for wiring purposes only.

Specified by:
getEventStore in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Returns:
the event store used by this fixture

getRepository

public Repository<T> getRepository()
Description copied from interface: FixtureConfiguration
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.

Specified by:
getRepository in interface FixtureConfiguration<T extends EventSourcedAggregateRoot>
Returns:
the repository used by this fixture


Copyright © 2010-2016. All Rights Reserved.