Kazed

Pragmatic software development

  • Increase font size
  • Default font size
  • Decrease font size
Home Java testing

Automatic testing of setters and getters


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.

Also, these setters and getters may contains trivial but nasty bugs that should be tested. It has happened (some years ago) that I incorrectly implemented a setter this way:

public void setName(String name) {
name = name;
}

I know, it's a pretty stupid mistake and still quite easy to make.

I created an automatic testing helper that will go through all methods of a class and if it detects a setter, invokes it with an appropriate test value and checks it by invoking the corresponding getter (if it exists). The return value of the getter should be the same as the value passed to the setter.



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! Mixx! Free and Open Source Software News Google! Live! Facebook! StumbleUpon! Yahoo! Free Joomla PHP extensions, software, information and tutorials.
Read more...