What are ways to keep hashCode/equals consistent with the business definition of the class?

后端 未结 6 2068
萌比男神i
萌比男神i 2021-02-01 19:12

Object javadocs and Josh Bloch tell us a great deal about how hashCode/equals should be implemented, and good IDEs will handle fields of various types correctly. Some discussio

6条回答
  •  余生分开走
    2021-02-01 20:06

    You could serialize your objects to a string using a tool that finds your properties using reflection (XStream, for example), and store that string in a repository. Your unit tests could re-serialize your objects and compare the results to your stored values.

    As part of your own process, make the storage of those strings contingent on manually validating that your hashCode and equals correctly capture all relevant values.

提交回复
热议问题