problems writing good test for axon for Bean Validation

We put javax validations on out command, and turned this on like:

commandBus.registerDispatchInterceptor(new BeanValidationInterceptor<>());

Now we would like to test that when a wrong command is created that an error is thrown. We try this by using the fixture:

Nu willen we ook graag testen dat als wij een foutief command aanmaken dat ook deze fout wordt gegooit. Dit proberen we zo:

AggregateTestFixture fixture = new AggregateTestFixture<>(Concept.class);

fixture.getCommandBus().registerDispatchInterceptor(new BeanValidationInterceptor<>());

fixture
.given(CREATED_EVENT)
.when(wrongCommand)
.expectException(JSR303ViolationException.class);

When we run this we see that an exception is throw but not in a way that our test is correct. Instead the Exception seems to break the test, while this is what we are trying ot test. We do get:

“org.axonframework.messaging.interceptors.JSR303ViolationException: One or more JSR303 constraints were violated:

property quantity in class replaceConcept must be between 1 and 999”

Does anyone have an idea what we are doing wrong or how we are able to test BeanValidationInterceptors neatly (not from our controllers)?

Cheers,

Bas

Hi Bas,

the fixture is intended to validate the behavior of the command model. That means the aggregate, its entities and possibly some external components required by this model. A Handler Interceptor is considered part of this behavior.

In your test, you are registering the BeanValidationInterceptor as a Dispatch interceptor. Since that is not considered part of the “system under test”, these exceptions bubble up to the test execution framework.

In your case, you can consider registering the interceptor as a Handler Interceptor instead. That will make it part of the “system under test” and validate exceptions as part of the command execution.

Cheers,