Unit testing for object immutability

前端 未结 6 2067

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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 21:32

    This thread can help How do I identify immutable objects in Java. Take a look at the second popular answer, it might be possible to check for any immutability problems with FindBugs. If you run it on every commit then you can call it a unit test :)

    EDIT

    It seems that FindBugs only check for final, that's not much. You could implement your own rule according to you patterns and classes which you use in the code.

提交回复
热议问题