Testing Aggreate Creation Command using GivenWhenThenFixture

Hi All,

When testing simple aggreate creation command I always encounter the following error:

java.lang.AssertionError: The aggregate used in this fixture was initialized with an identifier different than the one used to load it. Loaded [0], but actual identifier is [10000].
Make sure the identifier passed in the Command matches that of the given Events.
at org.axonframework.test.GivenWhenThenTestFixture$IdentifierValidatingRepository.validateIdentifier(GivenWhenThenTestFixture.java:435)
at org.axonframework.test.GivenWhenThenTestFixture$IdentifierValidatingRepository.load(GivenWhenThenTestFixture.java:429)
at org.axonframework.test.GivenWhenThenTestFixture$IdentifierValidatingRepository.load(GivenWhenThenTestFixture.java:411)
at org.axonframework.test.GivenWhenThenTestFixture.detectIllegalStateChanges(GivenWhenThenTestFixture.java:271)
at org.axonframework.test.GivenWhenThenTestFixture.when(GivenWhenThenTestFixture.java:236)
at org.axonframework.test.GivenWhenThenTestFixture.when(GivenWhenThenTestFixture.java:223)

I can get rid of above error by invoking FixtureConfiguration#setReportIllegalStateChange(false) but it seems wrong to do so since my aggregate is simple.

Below is the test code:
`
fixture.givenNoPriorActivity().when(new CreateEmployeeCommand(name, contact, joinedDate))
.expectVoidReturnType()
.expectEvents(new EmployeeCreatedEvent(10000, name,contact, joinedDate));

`

And below is the Aggregate:
`
class Employee extends AbstractAnnotatedAggregateRoot
{
private static final long serialVersionUID = 7125543543552936997L;
private final LongIdentifierGenerator GENERATOR = new LongGenerator(false, 10000);

@AggregateIdentifier
private long id;

@CommandHandler
public Employee(CreateEmployeeCommand command)
{
apply(new EmployeeCreatedEvent(GENERATOR.nextLongIdentifier(), command.getName(), command.getContact(), command.getJoinDate()));
}

@EventSourcingHandler
private void handleEmployeeCreatedEvent(EmployeeCreatedEvent event)
{
this.id = event.getId();
}
}

`

Any help would be greatly appreciated.

Thanks & Regards,
Setya

Hi,

I don’t have the means to check this right now, but I think the problem may be related to the ‘long’ identifier. Try changing it to a Long (object type). Since the primitive has no null value, Axon may falsely assume the identifier is initialized at 0.

Cheers,

Allard

Hi Allard,

Thank you. Changing into Long solves the problem. But then I’ve got 2nd error caused by expectVoidReturnType() :

The command handler returned an unexpected value.

Expected but got <10000>

Regards,

Setya

Hi Setia,

by default, the constructor command handler returns the id of the newly created aggregate. That’s why you get the 10000.

Cheers,

Allard

Hi Allard,

So for aggregate creation command I should always expect the generated id as return value instead of void?

Thanks & Regards,
Setya

Yes. Or just don’t validate the return value at all, unless you’re intending to rely on it.

Hi Allard,

Thanks for your help.

Regards,

Setya