How should one unit test the hashCode-equals contract?

前端 未结 8 1050
长发绾君心
长发绾君心 2020-12-07 13:18

In a nutshell, the hashCode contract, according to Java\'s object.hashCode():

  1. The hash code shouldn\'t change unless something affecting equals() changes
8条回答
  •  悲&欢浪女
    2020-12-07 13:33

    It's worth using the junit addons for this. Check out the class EqualsHashCodeTestCase http://junit-addons.sourceforge.net/ you can extend this and implement createInstance and createNotEqualInstance, this will check the equals and hashCode methods are correct.

提交回复
热议问题