View Full Version : How to test accesors and mutators?

05-02-2007, 08:32 PM
It's kind of interesting that I've had a hard time testing my classes to make sure its mutator and accesor methods work, quite simply because testing one requires the other and viceversa. I know that in test-driven development it is usually adviced to not test them because they are simple enough to be "trusted". However, when a member of my function is an array I feel uneasy about not testing the methods that are supposed to access and mutate it, since an array is no longer as simple as a variable. Then again my anxiety might just be typical in a newbie like me. I wonder though, if there is actually any method to test mutators, even if they are so intricately intertwined. They are quite simply supposed to be the only passageways of data in and out of the object. Should I just not test them? I definitely have always left out the possibility of making member variables public, even if just momentarily to test a mutator (for example) without having to use the accesor. I think it's not elegant, and not recomendable for continuous testing.

By the way I am programming in Visual Basic 6.0, something which I still find somewhat shameful, particularly because trying to do Object Oriented Programming is complicated :(

Thanks for the input!

05-03-2007, 03:38 AM
Well, one thing you can do is test the invariant of accessors:
setValue(v) -> getValue() == v if v is a valid value.
Of course, if you do some index calculation which just doesn't work, and copy&pasted it from one to the other, that's harder to detect.