«« Next » « Previous
«« Next » « Previous

Link Details

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.
Link 73561 thumbnail

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.
  • 6
  • 4
  • 1150
  • 379

Comments

Add your comment
User 191349 avatar

raveman replied ago:

-1 votes Vote down Vote up Reply

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.

User 254602 avatar

nwbrown replied ago:

0 votes Vote down Vote up Reply

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

User 279767 avatar

topbit replied ago:

0 votes Vote down Vote up Reply

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.

User 236137 avatar

dzonelurker replied ago:

0 votes Vote down Vote up Reply

Great! Will use it for my Unit Tests

User 281377 avatar

m0smith replied ago:

1 votes Vote down Vote up Reply

I like the idea but I modified it to take advantage of the Jakarta BeanUtils


public static void executeGetterSetterTest(Object bean) throws Exception {
PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bean);
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
String name = propertyDescriptor.getName();
if (PropertyUtils.isWriteable(bean, name)) {
Object testValue = getTestValue(PropertyUtils.getPropertyType(bean, name));
BeanUtils.setProperty(bean, name, testValue);
Object property = propertyDescriptor.getReadMethod().invoke(bean, new Object[0]);
Assert.assertEquals("Property '" + name + "' failed.", testValue, property);
}
}
}

I feel it makes it easier to read.

,
,

User 193149 avatar

p3t0r replied ago:

-1 votes Vote down Vote up Reply

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!

User 281968 avatar

koert.zeilstra replied ago:

0 votes Vote down Vote up Reply

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.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (6)



Voters Against This Link (4)