EventBus vs. EventTemplate in sagas

The documentation seems to suggest that I should prefer using EventTemplate to publish events. This works perfectly, but adds a small amount of friction to tests: no EventTemplate is injected by AnnotatedSagaTestFixture, so if I use EventTemplate in a saga class, in my test code I end up having to register a mock instance and test for calls to its publishEvent() method.

If I use the EventBus in my saga instead and handle wrapping my own objects in GenericEventMessage, my saga tests can use the fixture’s expectPublishedEvents() method to verify that the right events were sent, which feels cleaner.

Is this just a case of EventTemplate being a recent addition and the test fixture code not having caught up yet, or is the fixture’s API intentionally hinting to me that I should be directly calling the EventBus? If the latter, what’s the disadvantage of EventTemplate in the context of a saga?

Hi,

the EventTemplate has been added later. I guess it never made it to the fixtures as a default resource. You can easily register it as a resource: fixture.registerResource(new EventTemplate(fixture.getEventBus());

The advantage of EventTemplate is that it will synchronize publication of an event with the unit of work (if present). That means the event is raised only when the unit of work is committed (but before any transactions are committed). Also, the EventTemplate doesn’t require you to use the EventMessage interface, but just your own payload (and additional meta data).

Cheers,

Allard