File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Testing private method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Testing private method" Watch "Testing private method" New topic
Author

Testing private method

Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39
Hi All,

I have a method defined as private in my class. Now I want to create a test for it in JUnit. But I can't. Can somebody tell me why its not allowed in unit testing and wat is the wayaround.


Thanks
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

When you define a method of your class as private, nothing else than your class can use that method. You have restricted the access.
JUnit can't test your class, because you have forbidden JUnit to access it.

OCUP UML fundamental and ITIL foundation
youtube channel
Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39
Jan Cumps wrote:When you define a method of your class as private, nothing else than your class can use that method. You have restricted the access.
JUnit can't test your class, because you have forbidden JUnit to access it.


So what I am supposed to do in such a situation. Make all my methods public for testing purpose. And after the testing, change them back to private. But that is more prone to errors in two ways.

1. Some of the methods were left public after testing, which were originally private.
2. Secondly, code works fine when methods are public but problem occurs after I changed them back to private. Because of some access issue.

Kindly suggest

Abhilash

Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

I have no suggestion for you, Abhilash. I can explain why JUnit can't access them: the standard java access rules apply to JUnit too.

Some suggestions from others:
The JUnit FAQ on testing private messages.
Stackoverflow's discussion on the subject, with some comments on why and how to (not) do it.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30294
    
150

There are two common approaches:

1) Make them package private (no modifier)
2) Use reflection (via a library) to call them while remaining private.

I prefer #1.


[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
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Jeanne Boyarsky wrote:There are two common approaches:

1) Make them package private (no modifier)
2) Use reflection (via a library) to call them while remaining private.

I prefer #1.

Heh, I just came to this forums to ask exactly this question - what do you guys do if you have a private method that is complex enough to need to test it, but not really complex enough to extract it to a private collaborator and test it itself (yes, I do have such methods from time to time) - the latter seems a little too extreme at times.
I also came up with these 2 approaches, and also prefer loosing the access restriction to default.
Thank you for this comment.

Raf
Marcos R Oliveira
Ranch Hand

Joined: Apr 20, 2002
Posts: 51
Hi, Abhilash and Raf,

You could simply make sure your public methods are well tested, and hence, are your private methods, or you could use PowerMock.

Regards,
Marcos.


SCJP 1.4 - SCJP 1.6 - SCWCD in progress
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Sometimes this would mean an explosion of tests for the public methods, I think - sometimes the method might call more than one private methods, and then you have a cartesian product to test them properly. Or maybe it is just me and my bad design?
Marcos R Oliveira
Ranch Hand

Joined: Apr 20, 2002
Posts: 51
Hi,

I think that is expected to have several tests for a single public method, because the various scenarios we may encounter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing private method