Fixture test failed when a CommandHandler generate several events

935 views
Skip to first unread message

Philippe Da Costa

unread,
Mar 29, 2017, 9:25:28 AM3/29/17
to Axon Framework Users
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<String> 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: <f34ebebe-dcf2-495b-9700-433385b9ed9f>


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 :

Thanks for your help



jorgheymans

unread,
Mar 30, 2017, 2:33:27 AM3/30/17
to Axon Framework Users
Does MyAggregateId implement equals and hashcode correctly?

Nicolas Trichet

unread,
Mar 30, 2017, 4:57:22 AM3/30/17
to Axon Framework Users
I have forgotten the equals and hashcode methods in the sample project, I added it but it wasn't the source of the problem.

Allard Buijze

unread,
Apr 3, 2017, 8:17:59 AM4/3/17
to Axon Framework Users
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

--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages