aspose file tools*
The moose likes Testing and the fly likes The Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "The "JUnit and privates" debate" Watch "The "JUnit and privates" debate" New topic
Author

The "JUnit and privates" debate

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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...


[Jess in Action][AskingGoodQuestions]
Jason Rogers
Greenhorn

Joined: Aug 10, 2004
Posts: 1
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
J. B. Rainsberger
author
Ranch Hand

Joined: Aug 05, 2004
Posts: 87
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:

* Eclipse's "Extract Method" refactoring
* Legacy code

I have no problem making non-public methods public to test them. I have yet to see a compelling situation in which I would hesitate to do so, but sometimes you can't, and when you can't, use JUnitX.


Author of <a href="http://www.amazon.com/exec/obidos/ASIN/1932394230/ref=jranch-20" target="_blank" rel="nofollow">JUnit Recipes: Practical Methods for Programmer Testing</a>
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

How come the only choices are public vs private? What happened to making the methods/fields package-private so the tests can access them, but they are not really public.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
J. B. Rainsberger
author
Ranch Hand

Joined: Aug 05, 2004
Posts: 87
Jeanne:

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.
Greg Ostravich
Ranch Hand

Joined: Jul 11, 2002
Posts: 112
Maybe I don't hardly ever do this so that's why I'm confused but the JUnit FAQ gives instructions on how to do this using a PrivlegedAccessor class.

Is there a 'best practices' way to test private methods that involves using the Eclipse thing you mentioned or JUnitX?

Thanks for the insight on a method is either Open or Closed and everything else complicates matters. That was interesting. I guess it's like being sort of pregnant - you are or you aren't.


Greg Ostravich - SCPJ2
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

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?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Jeanne Boyarsky:
JB, Is not using package-private access for tests a best-practice or a personal preference?
I'd say personal preference, based on earlier discussions in several forums.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Thanks Lasse!
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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"
J. B. Rainsberger
author
Ranch Hand

Joined: Aug 05, 2004
Posts: 87
I don't believe in best practices. It is definitely personal preference, but I also think it's a pretty good practice.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

It is definitely personal preference, but I also think it's a pretty good practice.

Ok. Thanks. I disagree, but I guess that what makes it a preference

I created a thread in OO (as it isn't really about testing) because I am curious what others think on this topic.
[ August 12, 2004: Message edited by: Jeanne Boyarsky ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
J. B. Rainsberger:

I never use the package-level or protected access modifiers.

Are there no recipes in the book for testing protected methods? I find I really need such methods for code reuse within inheritance hierarchies.
J. B. Rainsberger
author
Ranch Hand

Joined: Aug 05, 2004
Posts: 87
No, there's no recipe for testing protected methods, although I probably do mention that placing test code in the same package as the production code allows access to protected/package access methods.
 
 
subject: The "JUnit and privates" debate