JUnit tests for POJOs

后端 未结 17 1669
Happy的楠姐
Happy的楠姐 2021-02-02 07:56

I work on a project where we have to create unit tests for all of our simple beans (POJOs). Is there any point to creating a unit test for POJOs if all they consist of is gette

17条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 08:23

    Unit tests not only validate that code works properly, but they document expected behavior. I don't know how many times I've seen things break because some time down the road, someone changes the behavior of a getter or setter in a POJO and then it unexpectedly breaks something else (for example, someone adds logic to the setter that if someone sets a null value on a string, the setter changes it to an empty string so that NPEs don't happen).

    I don't look at unit tests on data store POJOs as a waste of time, I see them as a safety net for future maintenance. That being said, if you are manually writing tests to validate these objects, you are doing it the hard way. Take a look at something like http://gtcgroup.com/testutil.html

提交回复
热议问题