Unit testing for object immutability

前端 未结 6 2079

I want to make sure that a given group of objects is immutable.

I was thinking about something along the lines of:

  1. check if every field is private final
6条回答
  •  暖寄归人
    2021-02-13 21:33

    I doubt you can do this with unit tests. The best way would be to be careful during writing the class or looking into the code. Precisely because of the problem that methods on the object can mutate its state which you might not see from the outside. Just because it's discouraged doesn't mean it doesn't happen :-)

提交回复
热议问题