Testing multiple event handlers in unit test?

All examples of axon testing I have seen, tests a single aggregate or event handler.
It is mostly a good thing to test one thing at the time, but sometimes I would like to test a couple of event handlers together.

I can see my program works, when I run it, but I don’t totally trust it, if I can’t unit test that event handlers play nice together.

I would like to unit test, two or more event handlers together.
One use case would be to send commands to the aggregate and test that the read model is updated correctly.

How is that possible?

Hi Lasse,

if you’re testing multiple things at once, it’s not really a Unit test any more…
But semantics aside, when you want to test components together, you will need to set up the Axon infrastructure for your test. You can do that by creating a Spring Context, or by using the Configuration API to quickly set up certain components.

Hope this helps.
Cheers,

Allard

I find much value in simple integration tests, that test two or three classes together. And a spring context (or worse @SpringBootTest) quickly gets out of hand for that.
But yes, let us not discuss test philosophy here. :wink:

Can you point me to some examples of how you test eventhandlers that are not aggregates?

I have one that publishes events and updates a query model.
I’d like to test it as elegantly as my aggregate test, but AggregateTestFixture assumes that the class under test is an aggregate.

Does something like a EventListenerTestFixture exist?

Hi Lasse,

there is no such thing as an EventListenerTestFixture. In fact, these handlers are pretty much regular methods, which you can just invoke in a test. If you want, you can wrap your bean in an AnnotationEventListenerAdapter, so that you can validate whether the annotations are correct, by invoking the handle(EventMessage) method.

If you want to test a few more components, I’d recommend using the Configuration API to quickly (both in dev-time and startup time) set up the basic infrastructure components.

Hope this helps.
Cheers,

Allard

Thank you.

Using the Configuration API is indeed very effective.

The downside of having everything handed to you in Spring Boot autoconfiguration, is that you as a developer don’t learn to use it - but you’ll probably need it anyway for testing.
So learning the configuration API is recommended to everyone who reads along.

As a community service here is a complete example of how to configure a event sourced aggregate, a read model and a command gateway.

EventHandlingConfiguration eventhandlers = new EventHandlingConfiguration()
.configureListenerInvocationErrorHandler(c -> PropagatingErrorHandler.instance())
.registerEventHandler(c -> myReadModel)

Configuration config = DefaultConfigurer.defaultConfiguration()
.configureEmbeddedEventStore(c -> new InMemoryEventStorageEngine())
.configureAggregate(MyAggregate.class)
.registerModule(eventhandlers)
.registerComponent(MyService.class, c -> new MyServiceStub())
.buildConfiguration();
config.start();

final CommandGatewayFactory factory = new CommandGatewayFactory(config.commandBus());
MyCommandGateway gw = factory.createGatewayMyCommandGateway.class);

Thanks for sharing that Lasse, I’ll definitely refer to this post when a similar request as yours arises!