Is it necessary to add (override) hashCode and equals methods to Axon aggregates and messages? Analysis in SonarQube shows that they are not used while doing standard Axon operations on these entities (in end to end tests). Moreover, during debugging the equals and hashCode methods do not seem to be reached as well.
1 Answers
Correct observation there Vadim.
From Axon's perspective there is only one spot where you'd have to add an equals()
function, which is an Aggregate Member which is contained in a list on an Aggregate (Root).
The axon-test
module's FixtureConfiguration
will match the Aggregate prior to handling a command and after it, to ensure the state hasn't been altered in the command handler. Axon uses a deep field comparison, unless an equals()
method is provided. A list of entities will incorrectly match the entities, as the object reference will be used if no equals()
is provided. It is thus required that your 'Aggregate Members' which you place in a list/set/map have an implementation of the equals()
function.
Note though that this is actually not ideal. Entities shouldn't be interchangable, as they are not fundamentally defined by their attributes (like Value Objects and Message) but rather by their identity and a thread of continuity. This is a known caveat which the axon-test
module is planned to cope with one day.
So, to answer you question in short: For testing having both is more than fair, but on a live system it's not necessary.

- 6,936
- 1
- 16
- 31