• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing private method

 
Abhilash Chander
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2599
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhilash Chander
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2599
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think that is expected to have several tests for a single public method, because the various scenarios we may encounter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic