Announcement
iPhone DZone: Fresh Links On Your iPhone by matt at Sat Jul 05 14:09:51 EDT 2008
Reading DZone on your iPhone just got a little bit easier. Visit http://dzone.com/iphone from your iPhone to try out our beta iPhone support. You can view stories, filter by tags, and login. We'd appreciate your feedback at feedback@dzone.com.
1% of the members rule this place. Be one of them! Login and vote now.
By mostlyharmless
via nowhere.kazed.net
Published: Apr 02 2008 / 10:36
Normally I do not bother testing setters and getters, they are usually too boring to expect any bugs lurking in there. I recently started working on a project that requires a test coverage of at least 75%, and I realized that to reach this percentage with classes that have relatively many setters/getters and only a little bit of other code, I wanted to test the setters and getters.
Comments
raveman replied ago:
nice idea, however that means that tests are not good. When you have good tests you dont care about that.
for example:
Cat cat = house.findCat(House.KITCHEN);
assertEquals(cat.getName(),"Garfield");
cat.setName("Garfield");
house.saveCat(cat);
// save check
checking for setters/getters is not needed in that example.
nwbrown replied ago:
Your getters and setters should be covered by calling the code that uses them. Thats where you will end up find the real bugs that they could be involved in. And if you don't have any code which uses them, that should raise a red flag (in fact, thats the real value I've seen in looking at code coverage, you often find unreachable or useless code that should be removed).
topbit replied ago:
100% targetted test coverage considered pointless.
As @nick says, above, If your tests aren't running through them, and retrieving the data without specifically testing a completely trivial function, then you have bigger problems.
dzonelurker replied ago:
Great! Will use it for my Unit Tests
m0smith replied ago:
I like the idea but I modified it to take advantage of the Jakarta BeanUtils
I feel it makes it easier to read.
,
,
p3t0r replied ago:
Please don't do stuff like:
"is" + method.getName().substring(3)
Have a look at the javabean packages:
java.beans.BeanInfo;
java.beans.IntrospectionException;
java.beans.Introspector;
java.beans.PropertyDescriptor;
You also completely 'forgot' te mention that if encapsulation is done right returned mutable objects shouldn't mutate their parent object... getters and setters are more complex then they seem at first sight!
koert.zeilstra replied ago:
The raised issues seem to me:
* "the setters and getters should be tested indirectly by code that uses your model class" - I think that the purpose of a unit test is that you test a unit in isolation, and you should not rely on other scattered unit tests.
* "x% targetted test coverage considered pointless." - I think so too, and unfortunately the company I work at does not agree and wants a coverage of 75%. Actually, we have this coverage tool and you have to set a standard somehow - without this number we would only have a vague requirement, which nobody would use. Now, we have a simple requirement and with some creativity also the means to achieve that goal.
In the article http://www.dzone.com/links/rss/getters_setters_and_the_great_coverage_conspiracy.html the author argues:
* "your code may have too many setters/getters" - I also think that classes with only setters and getters are just simple data structures that should be avoided, however, in practice I use EJB's, JSF and other frameworks that rely on the Java bean concept with setters and getters.
* "some people may test only the easy stuff and avoid the difficult code to achieve an easy code coverage percentage" - I absolutely agree, the goal of unit testing is ensuring quality, the percentage number is just an indication of that, not a replacement.,The raised issues seem to me:
* "the setters and getters should be tested indirectly by code that uses your model class" - I think that the purpose of a unit test is that you test a unit in isolation, and you should not rely on other scattered unit tests.
* "x% targetted test coverage considered pointless." - I think so too, and unfortunately the company I work at does not agree and wants a coverage of 75%. Actually, we have this coverage tool and you have to set a standard somehow - without this number we would only have a vague requirement, which nobody would use. Now, we have a simple requirement and with some creativity also the means to achieve that goal.
Voters For This Link (6)
Voters Against This Link (4)