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

Testing Private Methods

Serkan Demir
Ranch Hand

Joined: Feb 03, 2005
Posts: 61
Hi Everybody,

I dont want to add test methods in our production code, therefore i am using parallel source code trees for my test classes. But i am unable to test private methods.

What are your procedures for testing private methods?

Thanks lot,

Serkan


A guy from Turkey<br /><a href="http://blogspot.serkandemir.com" target="_blank" rel="nofollow">http://blogspot.serkandemir.com</a>
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What is your private method doing? Why do you feel the need to test it in isolation?

I typically find that if a private method is complex enough to need explicite testing, my design improves when I move that method to a class where it wants to be public.


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
Serkan Demir
Ranch Hand

Joined: Feb 03, 2005
Posts: 61
I have not a spesific private method, i am speaking of in general. I have dozens of private methods in my classes and i am wondering about there is any convenient method to test them without altering their visibility.
Is <b>reflection</b> a solution? I have read some tricks about their usage on private method testing.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
As Ilja said, if you have a private method complex enough that you feel it needs testing, you would probably do yourself a favor by extracting some of those private methods into a new class and make them public over there.

If you feel that's overkill, the next best thing you could do is probably to test your private methods through your public methods--they're the ones making use of those private methods anyway, right?

Finally, if you really really really don't want to extract your private methods elsewhere and testing through the public methods is somehow cumbersome (which is even more evidence for the need to extract a class from those private methods!), yes, you can use the Java Reflection API to invoke private methods from your test code. Google for PrivilegedAccessor or something like that.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Serkan Demir
Ranch Hand

Joined: Feb 03, 2005
Posts: 61
ok, i will apply one of your recommendations.
thanks lot,

serkan
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Also take a look at http://c2.com/cgi/wiki?MethodsShouldBePublic
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing Private Methods