Automagic unit tests for upholding Object method contracts in Java?

前端 未结 8 1362
傲寒
傲寒 2021-01-04 14:11

When developing Java applications, I often override Object methods (usually equals and hashCode). I would like some way to systematically check that I\'m adhering to the co

8条回答
  •  醉梦人生
    2021-01-04 14:46

    Maybe I'm misunderstanding the question (and being too CS), but it doesn't sound like the problem you're describing is decidable in the general case.

    In other words, the only way a unit test can assure you that an overriding method works the same on all inputs as the overridden method would be to try it on all the inputs; in the case of equals, that would mean all object states.

    I am not sure if any current test framework will automatically trim down and abstract the possibilities for you.

提交回复
热议问题