dcsimg
invoking private methods through JUNIT
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sonal_solat
Posted On:   Tuesday, April 16, 2002 10:18 PM

I am able to invoke private methods from another class using PrivilegedAccessor through JUNIT.But what if that private method returns some value. How do i get that value

Re: invoking private methods through JUNIT

Posted By:   Dave_Astels  
Posted On:   Thursday, April 18, 2002 09:11 AM

I advise against directly testing private methods.

I teach that unit testing should be blackbox, testing only public behaviour. This is a natural result of practising test driven development.

Private methods should be there primarily as a result of refactoring, and play a supporting role. They should not provide any real behaviour on their own.

Dave

Re: invoking private methods through JUNIT

Posted By:   Anonymous  
Posted On:   Thursday, April 18, 2002 01:43 AM

I haven't ever used PrivilegedAccessor, but there are some alternatives to think about:


  • don't test the method directly. If you test the methods that are calling the private one, it will get tested indirectly. This might be quiet sufficient.
  • make the method public. If the method is complex enough to need testing, it possibly belongs to an other class (a new one?), where it would be natural to make it public.
  • Use an inner class as testing probe. Inner classes have access to private members of the containing class. You can even remove the probing class for the production environment.


    class MyClass {
    private int myMethod() {
    ...
    }

    public class TestingProbe {
    public int myMethod() {
    return MyClass.this.myMethod();
    }
    }
    }


    Hope this helps...

  • About | Sitemap | Contact