0

I have read plenty of questions on here about overriding .equals and .hashcode for testing purposes only.

My Domain classes have implemented .equals and .hashcode. Should I be

1) Duplicating these classes in my test project

or

2) Using the Domain Class .equals in my test project

The only justification I can see for point one is if the .equals and .hascode are different in the test project, however in my case they are not. Therefore I believe that point two is the answer.

The reason I ask is that I am trying to follow the principle of least astonishment ready for when someone else looks at my code in future.

3
  • 2
    Not sure how you're going to duplicate the equals. Do you mean creating a inherited class for your SUT? Also not sure why you need to use the equal in your test. For equality assertion? Usually, people use custom assertion when they see repeating comparing logic in tests.
    – ivenxu
    Commented Jan 23, 2018 at 11:48
  • @ivenxu , The SUT is a domain object that overrides .Equals. I was planning to use that?
    – w0051977
    Commented Jan 23, 2018 at 11:54
  • 1
    "I have read plenty of questions on here about overriding .equals and .hashcode for testing purposes only." Really? What's the use-case for doing that?
    – David Arno
    Commented Jan 23, 2018 at 11:59

1 Answer 1

0

You already answered the question by yourself. If you need two different kind of equality comparisons somewhere in your code - tests or not - you obviously need to implement two different ones. And if you need only one, implement only one. The DRY principle applies to your whole code base, not just the code outside your tests.

Just make sure you override object.Equals for the comparison which feels most "natural" to the domain object, in the context of the particular program, and give the other equality check a self-describing function name which makes it clearly distinguishable from the former.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.