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 getters and setters? Is it a safe assumption to assume POJOs will work about 100% of the time?
Duplicate of - Should @Entity Pojos be tested?
See also
Is it bad practice to run tests on a DB instead of on fake repositories?
Is there a Java unit-test framework that auto-tests getters and setters?
I just started a project to do this. its in pre-alpha stage right now. It is an Eclipse plugin.
While I agree that typically a POJO setter/getter doesn't necessarily need a test, I believe it is nice to have the simple test there because as things change over time, it will make it easier for you to add more tests for the POJO's. The Unit test is set up, the methods are there to test the setter/getter, all you have to do is handle the new complexity.
This also helps with the code-coverage reports, which, helps keep managers happy. (My company uses Emma).
https://sourceforge.net/projects/unittestgplugin/
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 forif (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.
In my experience, creating unit tests for POJOs with only getters and setters, is just overkill. There are some exceptions, of course, if there is additional logic in the getter/setter like checking for null and doing something special, than I would create a unit test for that.
Also, if there's a bug in the POJO I'd create a unit test for it so we can prevent it from happening again.
I don't think there's a point to testing simple property getters and setters. The point of unit-testing is not to verify that your compiler works.
However, as soon as you add a conditional, null-check or other non-trivial behavior to your getters and setters (or other methods) I think it's appropriate to add unit tests.
It's probably worth a simple test to make sure you've not written
Although you should be coding to avoid that (by putting a
final
modifier on the method parameter, or similar). It also depends how you're generating your accessors, and if they could suffer from copy/paste errors etc (this will occur even in environments that try to enforce IDE usage - it just will).My main concern with classes containing lots of setters/getters, however, is what is the class doing ? Objects should do stuff for you, rather than just hold and return data. If these are data entity objects then the setter/getter pattern may be correct. However the better pattern is to set the data in the object, and ask the object to do stuff with it (calculate your bank balance, launch the rocket etc.) rather than return the data and let you do it yourself!
My answer is that trivial getters and setters do not merit their own tests. If I add any code other than simple reads or writes, then I add tests.
Of course, this is all boilerplate, so you could easily write a script that generates unit tests for your getters and setters, if you think there's any value there. Certain IDEs may allow you to define a template that creates test cases with test methods filled in for this boilerplate code (I'm thinking of IntelliJ here, but Eclipse can probably handle it too, although I haven't done anything like this in either IDE).