Monday, July 2, 2007

Unit testing setters and getters

This content has moved permanently to:

https://blog.jonm.dev/posts/unit-testing-setters-and-getters/



4 comments:

Things of No Interest said...

It's funny, I've been writing documentation for my company on unit testing, so here's what I wrote about why to test Javabeans:
- Conventions and coding practices may say not to put logic in your beans, but since you are not likely to be maintaining your code for its entire lifetime, you'll never be able to guarantee that somewhere down the line someone doesn't "break the rules" and misuse your code.
- You are trusting that a tool generated your code correctly.
- You can't claim full unit testing coverage if none of your beans are being tested.
- Because its ridiculously easy, so why wouldn't you?

There are plenty of ways to do it yourself, but the simplest thing I have found is a small open source utility from GTC group called TestUtil. It has some more robust features, but in its simplest form, your code looks like this:

public class SomeDataBeanTest extends TestCase {
public void testMutators() {
assertTrue("Getter/Setter test failed on: SomeDataBean.java", TestUtil.verifyMutable(new SomeDataBean()));
}
}

Jon Moore said...

Interesting. Do you have a link to TestUtil? It looks like it probably uses reflection rather
than explicitly writing out the tests; I'd be happy to look into it and follow up with a proper blog post about it.

Anonymous said...

I'm guessing it's this one:

GTC group called TestUtil.

Anonymous said...

I'm guessing it's this one:

http://gtcgroup.com/util/index.html