JUnit tests for POJOs

The rule in TDD is “Test everything that could possibly break” Can a getter break? Generally not, so I don’t bother to test it. Besides, the code I do test will certainly call the getter so it will be tested.

My personal rule is that I’ll write a test for any function that makes a decision, or makes more than a trivial calculation. I won’t write a test for i+1, but I probably will for if (i<0)... and definitely will for (-b + Math.sqrt(b*b - 4*a*c))/(2*a).

BTW, the emphasis on POJO has a different reason behind it. We want the vast quantity of our code written into POJOs that don’t depend on the environment they run in. For example, it’s hard to test servlets, because they depend upon executing within a container. So we want the servlets to call POJOs that don’t depend on their environment and are therefore easy to test.

Leave a Comment