views:

49

answers:

4

Is there a framework or library available that, when given a JavaBean, will "put it through its paces," i.e., test all the getters and setters, validate that a property matches the getters and setters, etc.?

+1  A: 

If you're not looking like something more fancy like http://commons.apache.org/validator/ I would recommend to write your own. Personally I don't like pure data holding objects without any behaviour - it's not very good design. So if I'm not forced to work with such objects (by working with j2ee f. e.), I try to get rid of them.

hackbert
+5  A: 

Personally, I don't think that's the hardest part of testing. It'd be possible to do via reflection, but that's not what makes testing worthwhile.

The hard part is figuring out all the possible inputs, for "happy path" and erroneous situations, making sure that exceptions are thrown when they should be, etc.

Your Java Bean should be implementing equals and hashCode. I'd worry more about tests to check the equals contract: null equals, reflexive, symmetric, transitive, and not equals. Those aren't trivial.

Getters and setters are the least of your problems. When people talk about code coverage standards of 70% or better they often say that getters and setters can be left out.

duffymo
Agreed that it's not the hardest part; just tedious. I worked for an organization that wrote a bean testing framework just to get code coverage numbers up.
Critical Failure
I think that's a false measure. High coverage does not mean good tests. Something like CRAP4J can be a better indicator.
duffymo
A: 

Take a look at Reflection Test Utilities:

http://code.google.com/p/rtu/

Although if you're testing generated methods (based on fields in a class) it may not be worth it.

Jon
This looks promising. Thanks!
Critical Failure
Voting the answer up would be good :)
Jon
A: 

You could try http://oval.sourceforge.net/ Oval it allows you to use annotations on your beans and then execute a validate method. It's not fully JSR303 compliant. If you want to use something fully compliant you should check out the Hibernate Validator.

Like the post above states definitely check out apache commons validator.

Michael Bazos