tags:

views:

1052

answers:

12

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?

+6  A: 

POJOs may also contain other functions, such as equals(), hashCode(), compareTo(), and various other functions. It may be useful to know that those functions are working correctly.

Kevin Crowell
These are also things I tend to test with POJOs. Not the accessors, but equals and hashCode and how the behave in different contexts, e.g. do they still work if the argument to equals is a Hibernate proxy etc.
Timo Westkämper
+5  A: 

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.

David Carlson
A: 

I think if the getters and setters have been created using an IDE then it should be fine. We have other things to put our code into. Obviously, you would test the POJO's for serialization/de-serialization.

daanish.rumani
Why? Are we testing the serialization mechanism? Unless you have customized the serialization this would be a waste of effort.
Robin
+1  A: 

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).

Jeff
+1  A: 

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.

Theo
+1  A: 

It's probably worth a simple test to make sure you've not written

public void setX(int x) {
   x = x;
}

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!

Brian Agnew
A: 

Unit Test code you want to know works (for the situations tested of course). Don't unit test code you only want to be kind of sure works.

I can't think of much I only want to be kind of sure about.

Nick Veys
+6  A: 

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.

Uncle Bob
If you're at a company that insists on a code coverage percentage, you could probably create a generic getter/setter test with BeanUtils from Apache commons. Just pass it the class, get all the getters and setter methods, use a little method name comparison to match them up.
Chris Kessel
A: 

I once spent two hours because of something like this:

int getX()
{
    return (x);
}

int getY()
{
    return (x); // oops
}

Since it takes almost no time to write the tests for simple getters, I do it now out of habit.

TofuBeer
A: 

I'm experimenting with cobatura for code coverage and just came across the same issue.

It would be nice to have an annotation to add to the class which said don't include this class in code coverage. However it is possible to put your pojo's in a separate package and then exclude that package from the analysis.

See the documentation depending on your tool, it works with Ant, Maven or command line.

+1  A: 

I use IntelliJ as an IDE, and it pretty much writes trivial POJO's for me - certainly the constructor and getters/settors. I don't see any point in unit testing this since any bugs will more than likely be in the test code I write.

Jon
A: 

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/

utcp-manager