I keep thinking back to a short comment at Bar Camp Manchester in the Unit Testing talk. When asked if anyone had written an app with 100% code coverage the guy beside me raised his hand. Now I’ve been advocating for a while now that it’s essentially wasted time to bother testing your getters and setters, and when I mentioned something to this effect he said, “How do you know you haven’t made a typo in a variable name?”
My mind just kind of spun for a second on that one for a second. I guess I’ve been in Java land for so long that the concept of having such a thing exist for more than a few seconds just doesn’t enter my brain. I mean first off, the IDE will bitch at you, because it can’t compile it, and even if you’re not using an IDE the compiler will bitch at you. I just responded that “I use a compiled language so that’s never something I have to worry about.”
Now, as many of you know I’m still a huge fan of languages like Perl and Ruby so I work in them fairly often. And I still say that it’s not worth your time writing tests for your getters and setters even in them because unlike Java these languages don’t have IDEs that happily spit out a getter and setter for every variable in your class regardless of if you need it or not. As a result, you only create the ones you need. And if you need them, then they’re going to be used, and if they’re used, you can pretty much guarantee that you’ll see the results of any typos show up really quickly when you test the methods that actually do something noteworthy, because they’ll use the getters and setters (otherwise why did you write them?) and either blow up when the compiler hits them or not work as expected, which is why you’re writing a test.