Class AxonTestFixture
- All Implemented Interfaces:
AxonTestPhase.Setup
Decorator chain architecture and the two-reference design
The fixture maintains two separate references for both the command bus and the event infrastructure:
- Outermost references (
commandBus,eventSink) — obtained from the configuration viaconfiguration.getComponent(...). These sit at the top of the decorator chain and are used by the given-phase and when-phase to dispatch commands and publish events. Dispatching through the outermost reference ensures that the message traverses all decorators, including dispatch interceptors that enrich messages with correlation metadata, tracing headers, etc. - Innermost recording references (
recordingCommandBus,recordingEventSink) — created byMessagesRecordingConfigurationEnhanceras the innermost decorators (DECORATION_ORDER = Integer.MIN_VALUE). These are used by the then-phase to assert on recorded messages. Because they sit at the bottom of the decorator chain, they capture messages after all dispatch interceptors have enriched them.
For commands, the decorator chain looks like:
commandBus (outermost, for dispatching)
→ InterceptingCommandBus (applies dispatch interceptors, enriches metadata)
→ recordingCommandBus (innermost, captures post-interceptor commands for assertions)
→ raw CommandBus implementation
For events, the same pattern applies. The concrete type depends on the configuration:
- With
EventSourcingConfigurer— anEventStoreis present, so the chain is:eventSink (outermost EventStore, for publishing) → InterceptingEventStore (applies dispatch interceptors) → RecordingEventStore (innermost, captures post-interceptor events for assertions) → raw EventStore implementation - With
MessagingConfigurer(no event sourcing) — anEventBusis present, so the chain is:eventSink (outermost EventBus, for publishing) → InterceptingEventBus (applies dispatch interceptors) → RecordingEventBus (innermost, captures post-interceptor events for assertions) → raw EventBus implementation (e.g. SimpleEventBus)
RecordingEventStore and RecordingEventBus implement RecordingEventSink, so they are
held uniformly as recordingEventSink regardless of the event infrastructure variant.
Why two references are necessary: If recording were at the outermost position, the recorder would capture the original, un-enriched message (before dispatch interceptors run). By placing recording at the innermost position, the recorder sees the fully enriched message — but we can no longer use the same reference for dispatching, because dispatching through the innermost reference would skip the interceptors. Hence the fixture keeps both: the outermost for dispatching and the innermost for assertions.
- Since:
- 5.0.0
- Author:
- Allard Buijze, Mateusz Nowak, Mitchell Herrijgers, Steven van Beelen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordAllow customizing the fixture setup. -
Constructor Summary
ConstructorsConstructorDescriptionAxonTestFixture(AxonConfiguration configuration, AxonTestFixture.Customization customization) Creates a new fixture. -
Method Summary
Modifier and TypeMethodDescriptiongiven()Transition to the Given phase to define the initial state of the system before testing.voidstop()Stops the fixture, releasing any active resources, like registered handlers or pending event processing tasks.when()Transition directly to the When phase, skipping the Given phase, which implies no prior state.static AxonTestFixturewith(ApplicationConfigurer configurer) Creates a new fixture.static AxonTestFixturewith(ApplicationConfigurer configurer, UnaryOperator<AxonTestFixture.Customization> customization) Creates a new fixture.
-
Constructor Details
-
AxonTestFixture
public AxonTestFixture(AxonConfiguration configuration, AxonTestFixture.Customization customization) Creates a new fixture.All components are resolved from the given
configuration:- The outermost
commandBusandeventSinkare used for dispatching commands and publishing events through the full decorator chain. - The innermost
recordingCommandBusandrecordingEventSinkare resolved via theRecordingComponentsRegistry(populated byMessagesRecordingConfigurationEnhancer) and used by the then-phase for assertions.
- Parameters:
configuration- The configuration to obtain components from.customization- Collection of customizations for this fixture.- See Also:
- The outermost
-
-
Method Details
-
with
Creates a new fixture.- Parameters:
configurer- The fixture will use the configuration build from the given configurer to obtain components needed for test execution.- Returns:
- A new fixture instance
-
with
public static AxonTestFixture with(ApplicationConfigurer configurer, UnaryOperator<AxonTestFixture.Customization> customization) Creates a new fixture.Registers a
MessagesRecordingConfigurationEnhancerthat places recording decorators at the innermost position of the decorator chain and registers them in aRecordingComponentsRegistry. The recording instances are resolved from the configuration when the fixture constructor obtains the registry viaconfiguration.getComponent(RecordingComponentsRegistry.class)and then reads the recordingCommandBus and recordingEventSink from it.- Parameters:
configurer- The fixture will use the configuration build from the given configurer to obtain components needed for test execution.customization- A function that allows to customize the fixture setup.- Returns:
- A new fixture instance
-
given
Description copied from interface:AxonTestPhase.SetupTransition to the Given phase to define the initial state of the system before testing.- Specified by:
givenin interfaceAxonTestPhase.Setup- Returns:
- A
AxonTestPhase.Giveninstance that allows defining the initial state.
-
when
Description copied from interface:AxonTestPhase.SetupTransition directly to the When phase, skipping the Given phase, which implies no prior state.- Specified by:
whenin interfaceAxonTestPhase.Setup- Returns:
- A
AxonTestPhase.Wheninstance that allows executing the test.
-
stop
public void stop()Description copied from interface:AxonTestPhase.SetupStops the fixture, releasing any active resources, like registered handlers or pending event processing tasks.- Specified by:
stopin interfaceAxonTestPhase.Setup
-