Hi,
I am trying to create the following scenario with axon 5: I have a command which create an aggregate (yeah, still using aggregates conceptually) that create my aggregate if it does not exist (emitting AggregateCreatedEvent), followed by an AggregateUpdatedEvent. But it seems that my EventSourcingHandler is not executed, therefore, sending the AggregateCreatedEvent even when the Event has already occured before.
this is my Aggregate
@EventSourced
public class SampleAggregate {
private UUID id;
private boolean created;
@EntityCreator
protected SampleAggregate(@InjectEntityId UUID id) {
this.id = id;
this.created = false;
}
@CommandHandler
public void createOrUpdateSampleAggregate(CreateOrUpdateSampleAggregateCommand cmd, EventAppender eventAppender) {
if (! created) {
eventAppender.append(new SampleAggregateCreatedEvent(cmd.getId()));
}
eventAppender.append(new SampleAggregateUpdatedEvent(cmd.getId()));
}
@EventSourcingHandler
private void created(SampleAggregateCreatedEvent event) {
this.created = true;
}
}
My testcase:
class SampleAggregateTest {
private AxonTestFixture fixture;
private final UUID id = UUID.randomUUID();
@BeforeEach
void setup() {
EventSourcingConfigurer configurer =
EventSourcingConfigurer.create().registerEntity(
EventSourcedEntityModule.autodetected(UUID.class, SampleAggregate.class)
);
fixture = AxonTestFixture.with(configurer);
}
@AfterEach
void tearDown() {
fixture.stop();
}
@Test
void createOrUpdate_whenPreExist() {
fixture.given().events(new SampleAggregateCreatedEvent(id))
.when().command(new CreateOrUpdateSampleAggregateCommand(id))
.then().success().events(new SampleAggregateUpdatedEvent(id));
}
}
I get the following error:
The published events do not match the expected events
Expected | Actual
--------------------------------------------------------|--------------------------------------------------------
be.hazeway.axon5.aggregate.SampleAggregateUpdatedEvent <|> be.hazeway.axon5.aggregate.SampleAggregateCreatedEvent
<|> be.hazeway.axon5.aggregate.SampleAggregateUpdatedEvent
org.axonframework.test.AxonAssertionError: The published events do not match the expected events
I notices that in my aggregate, the @EventSourcingHandler method was not triggered, hence the flag created was not set to true. Why was the @EventSourcingHandler method not triggered?
@Data
@AllArgsConstructor
@Event
public class SampleAggregateCreatedEvent {
private UUID id;
}