Given When Then Scenario
Single use Given/When/Then scenario runner and asserter
Assertions exceptions thrown from then_, thenExpectNoEvent, thenFailsWithException, thenFailsWithExceptionType are all subclasses of AssertionException
Example:
val scenario = GivenWhenThenScenario(ShipOrderDecider())
var orderId = OrderId.random()
scenario
.given(
OrderCreated(orderId),
OrderAccepted(orderId)
)
.when_(ShipOrder(orderId))
.then_(OrderShipped(orderId))And Decider example
class ShipOrderDecider : Decider<ShipOrder, OrderEvent> {
override fun handle(cmd: ShipOrder, events: List<OrderEvent>): OrderEvent? {
if (events.isEmpty()) {
throw RuntimeException("Cannot accept an order that hasn't been created")
}
if (events.any { it is OrderShipped}) {
// Already shipped - idempotent handling
return null
}
if (!events.any { it is OrderAccepted }) {
throw RuntimeException("Cannot ship an order that hasn't been accepted")
}
return OrderShipped(cmd.id)
}
}Properties
Functions
Set up the test scenario by providing past events that will be provided to the Decider.handle method as an event stream.
Define the expected event outcome when GivenWhenThenScenario is calling the Decider.handle with the command provided in when_ and past events provided in given
Define the expected event outcome when GivenWhenThenScenario is calling the Decider.handle with the command provided in when_ and past events provided in given
Define that we don't expect any events outcome when GivenWhenThenScenario is calling the Decider.handle with the command provided in when_ and past events provided in given
Define that we expect the scenario to fail with an expectedException of a given Exception instance when the GivenWhenThenScenario is calling the Decider.handle with the command provided in when_ and past events provided in given
Define that we expect the scenario to fail with an expectedExceptionType of a specific Exception type when the GivenWhenThenScenario is calling the Decider.handle with the command provided in when_ and past events provided in given
Define the command that will be supplied to the Decider.handle as the command when one of the then methods are called