Aggregate creation producing multiple events causing troubles with tests

Hi,

I’m new to Axon, so this may well be my issue, but I can’t see the way out.

I have an aggregate (eg “Account”) where creating a new instance via a @CommandHandler annotated constructor applies two events (eg “AccountCreatedEvent” and “AccountInvoiceGeneratedEvent”). The code (kotlin) is pretty unremarkable:

@CommandHandler

constructor**(command: CreateAccountCommand)** : this**() {**

val accountId = identifierFactory.generateIdentifier**()**

apply**(AccountCreatedEvent(**id = accountId,

name = command.name))

val invoiceId = identifierFactory.generateIdentifier**()**

apply**(AccountInvoiceCreatedEvent(**accountId = accountId,

invoiceId = invoiceId**))**

}

When testing this, I’m getting:

org.axonframework.eventsourcing.eventstore.EventStoreException:

Writing events for an unexpected aggregate. This could indicate that a wrong aggregate is being triggered.

Debugging the AggregateTestFixture code, the issue comes from the first event (the constructor) not having an aggregate identifier (which kind of makes sense - it doesn’t exist yet) but the second event does. The code checks:

if (!lastEvent.getAggregateIdentifier**().equals(event.getAggregateIdentifier())) {**

where “lastEvent” is the AccountInvoiceGeneratedEvent, and “event” is the AccountCreatedEvent.

I’m not sure if it’s a bug where the null check at AggregateTestFixture.java should also be checking for an empty string (getAggregateIdentifier() for AccountCreatedEvent returns an empty string rather than null) or am I doing something wrong?

Thanks,

Martin

Scratch that - the default value (empty string) was coming from my code… replacing it with a null String does the trick…

Hi Martin,

Great to hear you’ve figured out the problem yourself!

Things like this happen to everybody, so don’t feel to bummed out.

I hope to see more questions from you in the future here, as that means you’ll still be using Axon Framework! :slight_smile:

Cheers,
Steven