AggregateIdentifierNotInitializedException when writing GivenWhenThen fixture

Hello,

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.

The test code is:

`

@Test
public void voegFotoToe() {
    AlbumId albumId = AlbumId.create();
    BestandId bestandId = BestandId.create();
    VoegFotoToeCommand voegFotoToe = VoegFotoToeCommand.voegFotoToe(
            albumId,
            BestandId.create(),
            Paths.get("albumdir/foto"));

    FixtureConfiguration fixture = newGivenWhenThenFixture(Album.class);

    fixture.given(albumCreatedEvent(albumId))
            .when(voegFotoToe)
            .expectEvents(new FotoToegevoegdEvent(albumId, bestandId, Paths.get("albumdir/foto")));

}

`

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

Expected Actual
be.haezebrouck.triptrap.foto.domain.album.FotoToegevoegdEvent < >

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

`

I use Axonframework version 2.4.5

This is the complete code of my aggregate root:

`

package be.haezebrouck.triptrap.foto.domain.album;

import org.axonframework.commandhandling.annotation.CommandHandler;
import org.axonframework.eventhandling.annotation.EventHandler;
import org.axonframework.eventsourcing.annotation.AbstractAnnotatedAggregateRoot;
import org.axonframework.eventsourcing.annotation.AggregateIdentifier;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static be.haezebrouck.triptrap.foto.domain.album.AlbumCreatedEvent.albumCreatedEvent;

public class Album extends AbstractAnnotatedAggregateRoot {

    private static final Pattern VERJAARDAG_TITEL = Pattern.compile("^[Vv]erjaardag *(.+)$");

    @AggregateIdentifier
    private AlbumId albumId;

    private Album() {}

    @CommandHandler
    public Album(MaakAlbumCommand command) {
        AlbumCreatedEvent.AlbumCreatedEventBuilder createdEvent =
                albumCreatedEvent(command.albumId())
                .withAlbumNaam(command.albumNaam());

        Matcher verjaardagMatcher = VERJAARDAG_TITEL.matcher(command.albumNaam().titel());
        if (verjaardagMatcher.matches()) {
            createdEvent.verjaardag(verjaardagMatcher.group(1));
        }

        apply(createdEvent.build());
    }

    @CommandHandler
    public void voegFotoToe(VoegFotoToeCommand voegFotoToe) {
        FotoToegevoegdEvent fotoToegevoegd = new FotoToegevoegdEvent(
                albumId,
                voegFotoToe.bestandId(),
                voegFotoToe.relativePath());
        apply(fotoToegevoegd);
    }

    @EventHandler
    public void on(AlbumCreatedEvent event) {
        this.albumId = event.albumId();
    }
}

`

Strange problem, because I do a similar thing with another aggregate, without problems

Aha, I found the issue.
PROBLEM SOLVED

It was just me :slight_smile: (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:

`

@Test
public void voegFotoToe() {
    AlbumId albumId = AlbumId.create();
    BestandId bestandId = BestandId.create();
    VoegFotoToeCommand voegFotoToe = VoegFotoToeCommand.voegFotoToe(
            albumId,
            **bestandId**,
            Paths.get("albumdir/foto"));

    fixture.given(albumCreatedEvent(albumId)**.****build****()**)
            .when(voegFotoToe)
            .expectEvents(new FotoToegevoegdEvent(albumId, **bestandId**, Paths.get("albumdir/foto")));
}

`

This very little project, developed in my spare time to play around with Axon (and show it to my Cegeka colleagues), can be checked out at: https://gitlab.com/stijnhaezebrouck/triptrap-foto.git

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.

Cheers,

Allard