Tests fail when using commons-lang equals and hashCode with reflection

We are using the commons-lang equals, hashCode with reflection on the objects in our project. All our axon tests fail the equality test when we are using reflection.

@Override

public int hashCode() {

return HashCodeBuilder.reflectionHashCode(this);

}

@Override

public boolean equals(Object obj) {

return EqualsBuilder.reflectionEquals(this, obj);

}

@Override

public String toString() {

return ToStringBuilder.reflectionToString(this);

}

We can get the tests to work if we don’t use the reflection.

There is this comment in the EqualsBuilder documentation.

Because these fields are usually private, the method, reflectionEquals, usesAccessibleObject.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than testing explicitly.

Is there something specific about how Axon is doing the compare of the aggregates in it’s testing framework that would prohibit the use of reflection?

Carol

Hi Carol,

Axon does nothing special with classloading or security managers.
The test fixtures use equals and hashcode to compare the actual and expected events. It does the same with the aggregate to detect state changes by command handlers that aren’t reflected by an event.
However, if an object does not override the equals method from Object, Axon uses reflection to compare the fields of the object, with the same rules.
My guess is that it has to do something with this mechanism. Did you try to implement a more ‘traditional’ equals and hashCode?

Cheers,

Allard