Hi,
I’ve some concerns about @EventHandler in AR.
I know that Axon Fixtures makes testing easy and readable but they don’t test the persistence of changes that we are applying in @EventHandler sections.
Consider the example from axon-trader example app:
In module orders we have class Portfolio that publishes the CashReservedEvent. There are some business rules that are not allow to reserve cash if amountOfMoney >= amountToReserve. We can see that here:
public void reserveMoney(TransactionId transactionIdentifier, long amountToReserve) {
if (amountOfMoney >= amountToReserve) {
apply(new CashReservedEvent(portfolioId, transactionIdentifier, amountToReserve));
} else {
apply(new CashReservationRejectedEvent(portfolioId, transactionIdentifier, amountToReserve));
}
}
And according to that there is related @EventHandler
@EventHandler
public void onMoneyReservedFromPortfolio(CashReservedEvent event) {
amountOfMoney -= event.getAmountToReserve();
reservedAmountOfMoney += event.getAmountToReserve();
}
And that looks quite neat and clean. All test are working. But when I change the @EventHandler implementation to:
@EventHandler
public void onMoneyReservedFromPortfolio(CashReservedEvent event) {
amountOfMoney += event.getAmountToReserve();
reservedAmountOfMoney -= event.getAmountToReserve();
}
I violate business rule that **amnountOfMoney** cannot be less than zero. But all tests still works.
My question here is how to handle with that problem? Lastly I simply missed to implement the **@EventHandler** and was wondering why my changes are not applied to the AR ;)
Only solution that came to my head is to test such case:
given:
20 amount of money
10 as amount to reserve
when:
15 as amount to reserve
then:
expect CashReservationRejectedEvent
Here the simulation is quite easy, but I can imagine more complex business rules that will be harder to simulate.
Or simply forget to implement the proper **@EventHandler** (I commented out the **@EventHandler** that handles the **CashReservedEvent** and all test are still green)
Does anyone have same or similar problem with that?
m.
`
`