Inside test method
Below repository is set inside the test method, and using fixture setting the previous state directly, not using the events, before initiating a new command on the prev state.
EventSourcingRepository repository = CachingEventSourcingRepository.builder(SampleAggregate.class)
.eventStore(fixture.getEventStore())
.build();
externalCommandHandler.setRepository(repository);
//...
//...
fixture.givenState(() -> previousState)
.andGivenCommands(expectedCommand1, expectedCommand2)
.when(expectedCommand3)
.expectSuccessfulHandlerExecution()
.expectEvents(expectedEvents)
.expectState(actualAgg -> {
//...
});
Inside command handler
private EventSourcingRepository<SampleAggregate> repository;
public void handle(SampleCommand command){
repository.load(command.getAggregateId())
.execute(agg->{
// ..
});
}
Error
At runtime getting this error, and its valid becuase there are no events. But how to get the aggregate instance set as prev state in test method when calling repository.load()
Caused by: org.axonframework.modelling.command.AggregateNotFoundException: No 'given' events were configured for this aggregate, nor have any events been stored.
at org.axonframework.test.aggregate.AggregateTestFixture$RecordingEventStore.readEvents(AggregateTestFixture.java:850)
at org.axonframework.eventsourcing.EventSourcingRepository.readEvents(EventSourcingRepository.java:147)
at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:124)
at org.axonframework.eventsourcing.EventSourcingRepository.doLoadWithLock(EventSourcingRepository.java:52)
at org.axonframework.modelling.command.LockingRepository.doLoad(LockingRepository.java:118)
at org.axonframework.modelling.command.LockingRepository.doLoad(LockingRepository.java:52)
at org.axonframework.modelling.command.AbstractRepository.lambda$load$4(AbstractRepository.java:122)
at java.util.HashMap.computeIfAbsent(HashMap.java:1127)
at org.axonframework.modelling.command.AbstractRepository.load(AbstractRepository.java:121)
at org.axonframework.modelling.command.AbstractRepository.load(AbstractRepository.java:168)