Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

minimum access level vs. test-suite...

 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello from Berlin,

I'm working on my certification code, and just came to this basic question:

obviously...
(1) it's good coding style to use the minimumm access level for all class fields & methods.
(2) in order for a field / method to be tested, it cannot be private.

in daily business, we don't bother very much. of course, we have private methods, which are tested indirectly (that's why it's called _unit_test...). but if i want a certain method to be tested explicitly (say because it's funtionality deserves explicite testing), i give it a less restrictive level, and that's fine.
overall a huge benefit for code quality.

BUT as we are not deploying our testsource, a revisor could have a look on my classes and find methods and fields neither being private nor being used from outside.

well, i think i'm going to document this process in my code, as in the design-decision document. but i'm curious how you guys solved this problem...

so, what do you think, "testing quality" vs. "interface clearity"?

thanks for any input,
jan
 
Wim van Haaren
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jan,

Go for the "interface clearity".
Unit tests are meant for testing the interface of a class, which means the public (or protected) methods. When a (correct) unit test succeeds it means that the class is behaving as specified by its interface. In other words, a unit test is supposed to test if the class honours its contract. Private methods therefore are not of interest and should not be tested directly.

It is bad OO practice to change the visibility of methods just for the purpose of testing (making fields accessible is a real no-no, unless it is a constant (public static final)).

I'm afraid that if you make methods less restrictive for the sole purpose of testing, you might receive a deduction in your score for OO design.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic