Fixture test failed when a CommandHandler generate several events

Hi,

We have wrote a CommandeHandler that generate several command to create several aggregates.

`
@Component
public class MyFirstCommandHandler
{
private final CommandGateway commandGateway;

public MyFirstCommandHandler(CommandGateway commandGateway)
{
this.commandGateway = commandGateway;
}

@CommandHandler
public void handle(
final InitCommand initCommand)
{
// for example call external api that returns a list of objects to create
List response = asList(“a”, “b”);
response.forEach(s -> commandGateway.send(new CreateMyAggregateCommand(new MyAggregateId(), s)));
}
}
`

The aggregate

`

@Aggregate
@NoArgsConstructor
public class MyAggregate
{
@AggregateIdentifier
private MyAggregateId id;

private String something;

@CommandHandler
public MyAggregate(final CreateMyAggregateCommand command)
{
apply(new MyAggregateCreatedEvent(command.getId(), command.getSomething()));
}

@EventSourcingHandler
public void on(final MyAggregateCreatedEvent event)
{
this.id = event.getId();
this.something = event.getSomething();
System.out.println(event);
}
}

`

And the test corresping to the first command handler

`

public class MyFirstCommandHandlerTest
{
private FixtureConfiguration fixture;

@Before
public void setUp()
throws Exception
{
fixture = new AggregateTestFixture<>(MyAggregate.class);
CommandBus commandBus = fixture.getCommandBus();
DefaultCommandGateway commandGateway = new DefaultCommandGateway(commandBus);
fixture.registerAnnotatedCommandHandler(new MyFirstCommandHandler(commandGateway));
}

@Test
public void testMigrateStream()
throws Exception
{
fixture
.givenNoPriorActivity()
.when(new InitCommand())
.expectSuccessfulHandlerExecution();
}

}

`

The test fail because with the error

`

org.axonframework.test.AxonAssertionError: Illegal state change detected! Property “test.onecommandtwoevents.domain.MyAggregate.id.identifier” has different value when sourcing events.
Working aggregate value: <8802e90d-2764-4daa-97c5-d661ba38918e>
Value after applying events:

`

We don’t know why but the framework try to apply the first emited event to a third instance of the aggregate !!
But when the application run’s normally, we do not have errors.

We have created a minimal project to show the use case :

https://github.com/micolator/test-axon-one-command-two-events

Thanks for your help

Does MyAggregateId implement equals and hashcode correctly?

I have forgotten the equals and hashcode methods in the sample project, I added it but it wasn’t the source of the problem.

Hi Philippe, Nicolas,

the AggregateTestFixture is not designed to work with multiple aggregate instances. In some cases, it may work fine, but you should note that the “given” events may be applied incorrectly to the different aggregate instances.

In your case, what’s failing, it the illegal state change detection. It detects a different identifier on the loaded aggrgeate than on the runtime one. This is most likely due to events of multiple aggregate being in the fixture’s “event store”.

Switching illegal state change detection off (setReportIllegalStateChange(false)) may solve the issue. But beware, multi-aggregate is still an unsupported feature that may (or may not) accidentally work…

Cheers,

Allard