• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Testing Private Methods

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Serkan Demir
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, i will apply one of your recommendations.
thanks lot,

serkan
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also take a look at http://c2.com/cgi/wiki?MethodsShouldBePublic
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic