J.B., where do you stand on the eternal debate about testing private methods? On one end of the spectrum are the "only test the public interface" folks, and on the other end are the "test everything, even if it means opening up methods that might otherwise be private" folks (and the "nothing should be private" folks, too.) Or there are those ugly reflection tricks, of course; surely you wouldn't advocate using those...
I won't answer for J.B., but hopefully you mind if I chime in...
From Sun's documentation: , "A private member is accessible only to the class in which it is defined." Therefore, anything else is public, in some sense of the word.
The public interfaces must somehow act upon the private members, otherwise you wouldn't have them. There must be some noticeable affect on the system when a private member is changed, test that.
If you have no way of measuring/testing the effect on the system when a private member is changed you have a design flaw. If you are able, you should update the design accordingly, if not I guess you have to fall back on reflection or some other work around. My preference has always been to heed my tests by updating the design.
"I am crucified with Christ: nevertheless I live; yet not I,<br /> but Christ liveth in me: and the life which I now live in <br /> the flesh I live by the faith of the Son of God, who loved <br /> me, and gave himself for me."<br /> Galatians 2:20
JUnit Recipes includes some advice on testing private methods using JUnitX, but you'll notice the recipe title is "Test a private method if you must".
To me, there's no great debate. I prefer not to test private methods, because that requires real work. If I must test a private method, because I cannot change the code under test, but I nevertheless am worried it might fail, then I go ahead and test it. For me, there are two main ways I get private methods:
I never use the package-level or protected access modifiers. I just find that I never need those levels of protection. To me, part of a class is either open or closed. Various degrees of "open" serve only to complicate my life, and my life is complicated enough.
I wouldn't use package-private access for any purpose other than testing (although some of my coworkers disagree.) I find that it creates more readable tests than using reflection/junix to access privates.
JB, Is not using package-private access for tests a best-practice or a personal preference?
Originally posted by Lasse Koskela: The best argument so far for not using package-private access has been "the method signatures look stupid in my IDE if there's no access modifier"
Oh, yeah, it really does! :roll:
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus