Unit testing for object immutability

前端 未结 6 2073

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:23

    If you generate your data model and all its code, you can ensure the possible Data Value objects you create will be immutable to meet your needs.

    The problem you have is that there is different forms of immutability. Even String would fail your test Are String, Date, Method immutable? You can prove that a class is strictly immutable this way, but you are likely to be better off generating your data model.

提交回复
热议问题