I am testing the effect (expected events) when I execute a command on my aggregate root (Album, extends AbstractAnnotatedAggregateRoot). I provide the initial event (which should set the aggregateRootId) but still getting an AggregateIdentifierNotInitializedException.
albumCreatedEvent returns an event normally apply by the command passed in in the aggregate constructor. When albumCreatedEvent is applied, the aggregateRootId is set.
As I am testing “VoegFotoToeCommand”, the creation event should already be there and applied. But the fixture does return the AggregateIdentifierNotInitializedException… why?
The following is the a part of the error stacktrace:
`
org.axonframework.test.AxonAssertionError: The published events do not match the expected events
A probable cause for the wrong chain of events is an exception that occurred while handling the command.
org.axonframework.domain.AggregateIdentifierNotInitializedException: AggregateIdentifier is unknown in [be.haezebrouck.triptrap.foto.domain.album.Album]. Make sure the Aggregate Identifier is initialized before registering events.
at org.axonframework.domain.AbstractAggregateRoot.getEventContainer(AbstractAggregateRoot.java:174)
org.axonframework.test.AxonAssertionError: The published events do not match the expected events
It was just me (you could not have spotted it from the code I posted here)
The code fixture.given(albumCreatedEvent(albumId))
was in fact not returning an AlbumCreatedEvent (to which the aggregate was listening), but an AlbumCreatedEventBuilder. Axon will accept any object as an event, and will not complain is no-one is listening for it. So the builder is accepted as an event.
Then, for the reader that has spotted it, there was another issue: the bestandId in the command was regenerated (BestandId.create())
twice, so that the id in the expected event in the test would always differ from the one put in the command.
Even while I am glad I found the problem myself, it nice to have a group like this to post questions related to Axon.
The corrected, working test code in this case is now:
Glad you found it. The code looked all right.
But I guess the error message would be even more helpful if it contained the name of the first event, which is expected to set the aggregate identifier. Will have a look if I can inprove on that.
If you need more help convincing any of your colleagues, don’t hesitate to drop em a note.