This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Testing and the fly likes Unit testing-private methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Unit testing-private methods" Watch "Unit testing-private methods" New topic
Author

Unit testing-private methods

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

What do the methods do? Can you test them through how they are called by the public methods?


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

Joined: Jan 23, 2002
Posts: 11962
    
    5
Rule #1 of unit testing: Test behavior, not methods.


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

Joined: Dec 29, 2005
Posts: 308
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).


aldana software engineering blog & .more
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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 ]

Rahul Bhattacharjee
LinkedIn - Blog
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jul 11, 2001
Posts: 14112
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.


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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unit testing-private methods
 
Similar Threads
Testing private method
B&S (2.3.1) - Passed 375/400
minimum access level vs. test-suite...
best practises
testing private methods with junit?