sagaVerifier

fun <T : Any> Class<T>.sagaVerifier(serviceProvider: ServiceProvider = SimpleServiceProvider(), commandGateway: CommandGateway = defaultCommandGateway(), commandMessageFactory: CommandMessageFactory = SimpleCommandMessageFactory( validator = TestValidator, commandBuilderRewriterRegistry = SimpleCommandBuilderRewriterRegistry(), )): WhenStage<T>

Creates a when stage for testing a stateless saga of this class type.

This extension method on Class creates a complete test environment for the specified saga class, allowing you to define test scenarios using the fluent DSL. It automatically extracts processor metadata from the class annotations and sets up all necessary components for saga testing.

The method configures:

  • Saga processor metadata extraction

  • Service provider for dependency injection

  • Command gateway for sending commands triggered by the saga

  • Command message factory for creating command messages

Example:

CartSaga::class.java.sagaVerifier()
.whenEvent(
event = mockk<OrderCreated> { ... },
ownerId = "owner123"
)
.expectCommandType(RemoveCartItem::class)
.expectCommand<RemoveCartItem> { body.productIds.assert().hasSize(1) }
.verify()

Return

a WhenStage instance for defining test scenarios with domain events

Parameters

T

the type of the saga class to be tested

serviceProvider

the service provider for dependency injection, defaults to SimpleServiceProvider

commandGateway

the command gateway for sending commands, defaults to defaultCommandGateway()

commandMessageFactory

the factory for creating command messages, defaults to SimpleCommandMessageFactory with TestValidator

Throws

if saga metadata cannot be resolved from class annotations


inline fun <T : Any> sagaVerifier(serviceProvider: ServiceProvider = SimpleServiceProvider(), commandGateway: CommandGateway = defaultCommandGateway()): WhenStage<T>

Creates a when stage for testing a stateless saga using reified generics.

This inline function provides a convenient way to create saga verifiers without explicitly specifying the Class type. It leverages Kotlin's reified generics to automatically infer the saga class type at compile time, enabling type-safe testing without reflection overhead.

This is the recommended method for creating saga verifiers in Kotlin code due to its type safety and conciseness compared to the Class-based extension method.

Example:

sagaVerifier<OrderSaga>()
.whenEvent(
event = mockk<OrderCreated> {
every { items } returns listOf(orderItem)
every { fromCart } returns true
},
state = mockk<OrderState>()
)
.expectNoCommand()
.verify()

Return

a WhenStage instance for defining test scenarios with domain events

Parameters

T

the reified type of the saga class (inferred automatically)

serviceProvider

the service provider for dependency injection, defaults to SimpleServiceProvider

commandGateway

the command gateway for sending commands, defaults to defaultCommandGateway()

Throws

if saga metadata cannot be resolved from class annotations