• 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

Unit testing-private methods

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

We have many methods in a class that are private....and few public....


In unit testing..we can test the public methods...but what about the private methods...how can we validate those methods....

Regards
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do the methods do? Can you test them through how they are called by the public methods?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rule #1 of unit testing: Test behavior, not methods.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

lasse is right. that's why i stay away from generating test-case tools, which often generate test-skeletons of public methods.
as an example: think about a login() method. it does not make sense to brutely just generate one testMethod testLogin(). you should look at the behaviour and create test-cases like testLoginSuccessful(), testLoginUserNotExists(), testLoginPasswordWrong() etc.. as you can see one public method often maps to different test-cases.

apart from of that there are tools which make testing private methods possible. but i would stay away from that: see your need to test private methods as a code/design smell to refactor (extract class).
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
Rule #1 of unit testing: Test behavior, not methods.



Adding to it , Lasse Koskela please correct me if I am wrong.

Behaviors for use are exposed through public methods only and the private methods would support the exposed behavior , then why to test private methods ?

Just found this while going through the FAQ.
http://faq.javaranch.com/view?UnitTestingPrivateMethods
[ July 26, 2007: Message edited by: Rahul Bhattacharjee ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:
Behaviors for use are exposed through public methods only and the private methods would support the exposed behavior , then why to test private methods?


Exactly. If the private method is not used indirectly by the public methods, shouldn't you delete those private methods as they're essentially dead code?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Bhattacharjee:

Behaviors for use are exposed through public methods only and the private methods would support the exposed behavior , then why to test private methods ?



One reason could be that the private method is very complex, and it's quite hard to test it through the public interface of the class.

The answer is, of course, that this is a code smell - such a complex method belongs on a class where it'd be naturally public.
 
reply
    Bookmark Topic Watch Topic
  • New Topic