• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unit testing-private methods

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34237
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the methods do? Can you test them through how they are called by the public methods?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rule #1 of unit testing: Test behavior, not methods.
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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).
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • 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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic