• 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

Testing private method

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
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
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.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic