unit test with aggregate factory (Axon 2.4.1)

Hi,

I’ve run into problems using given-when-then fixtures with AggregateFActory. Details follow but tl;dr version is - fixture too eagerly checks class name of the aggreate.

Inside @Before method:
`
ProductAggregateFactory factory = new ProductAggregateFactory();
EventSourcingRepository repository = new EventSourcingRepository<>(factory, fixture.getEventStore());

fixture.registerRepository(repository);
fixture.registerAnnotatedCommandHandler(productCommandHandler);
fixture.registerAggregateFactory(factory);
`

Working test:
`
fixture.getCommandBus().dispatch(new GenericCommandMessage<>(createProductCmd));
fixture.getCommandBus().dispatch(new GenericCommandMessage<>(commandInstanceOne));
fixture.getCommandBus().dispatch(new GenericCommandMessage<>(commandInstanceTwo));

try (CloseableUnitOfWork ignored = CloseableUnitOfWork.startAndGet()) {
BaseProduct product = fixture.getRepository().load(intId);
// assert that commandOne and Two had effects
}

`

Failing test:
`
fixture.givenCommands(createProductCmd, commandInstanceOne)
.when(commandInstanceTwo)
.expectEvents(new eventTwo());

`

And the exception I get:

`
ProductEventTests.testAdditionalInfoWithFixture:161 The aggregate loaded based on the generated events seems to be of another type than the original.
Working type: <pl.example.project.BaseProduct>
Event Sourced type: <pl.example.project.ChildClass>

`

ChildClass extends BaseProduct.
Exception comes from method org.axonframework.test.GivenWhenThenTestFixture#assertValidWorkingAggregateState, maybe it should test isAssignable>From instead of .equals?

Hi,

does the CreateProductCommand create an instance of the “ChildClass”? If the command creates a different instance type than the factory does based on the event (generated by that same command), the fixture will report this as an error.

Meanwhile, I will also check the sources for any eager instance checks.

Cheers,

Allard

PS. Sorry for the late response. I’m in an area where Internet apparently isn’t a commodity yet.

Hi Kamil,

I have checked the code, and there is no eager type checking. In fact, it seems like Axon is correctly reporting this as a potential issue. You createProductCmd most likely creates a BaseProduct instance. However, your AggregateFactory will create another type of Aggregate based on the events generated by the CreateProductCmd. Since an event sourced instance is different from the instance created by the commands, Axon will fail the test.

You can switch of the instance checks by calling setReportIllegalStateChange(false) on the FixtureConfiguration.

Cheers,

Allard

Hi Allard,
thanks for the response and pointing out this setReportIllegalStateChange.

I don’t really understand what do you mean by “does the CreateProductCommand create an instance of the “ChildClass”?”.
“Create” command is handled only by the command handler external to the aggregate; I don’t have any @CommandHandler methods on the Aggregate itself.
So from the beginning there should only be an instance of a specialized “ChildClass”, not the Parent.

Regards,
Kamil

Hi Kamil,

the test fixture, under the hood, uses "given , when , then ". Even wen you use given . In that case, it will check the events that were generated as part of that command, and use that to build the state of the aggregate to use for the "when " operation.

As soon as I have a little more time on my hands, I will try to set up a small test to see if the issue is reproducible.

Cheers,

Allard