Testing a @ExceptionHandler with a Saga

Hi there,

Is it possible to test a @ExceptionHandler annotated method used in a Saga.
The test uses a SagaTestFixture and it looks to me the fixture is not scanning for this annotation to register a handler.

Then probably I need to make it myself?

Thnx in advance!

I don’t have a definitive answer with example code.

But if you check the docs for the saga test fixture (https://apidocs.axoniq.io/4.4/org/axonframework/test/saga/SagaTestFixture.html) and search for “exception” you will find the following method:

registerListenerInvocationErrorHandler(ListenerInvocationErrorHandler listenerInvocationErrorHandler)

Which is described as:

Registers a ListenerInvocationErrorHandler to be set for the Saga to deal with exceptions being thrown from within Saga Event Handlers.

You may find this method useful (or check the docs for an alternative method which may suit you better).

There’s indeed no dedicated testing support for the @ExceptionHandler annotation. Neither for the Sagas or the Aggregates in fact.

@vab2048 his suggestion with the ListenerInvocationErrorHandler would work though actually. Through this approach, you can define a custom ListenerInvocationErrorHandler which asserts the exception being thrown from within your @ExceptionHandler. If nothing happens, you’d assert that the ListenerInvocationErrorHandler woudln’t be reached.

Okay, I’ll just use a try-catch instead.

As the @ExceptionHandler won’t be invoked during testing or I have to write my own ErrorHandler and set it up for testing.

This would mean even more code to write :slight_smile: