Search code examples
javareflectionmethodsjunitprotected

JUnit Testing: invoking parent class protected method


Here is my class hierarchy

abstract Class A
{
   int i = 0;
   protected init (String param1, String param2)
   {
       //do lots of common things based on param1, param2 and save data in i
   } 
}

Then there are 3-4 classes which implements A i.e

Class B extends A
{
    public B ()
    {
        super ();
    }

    public void performSomeAction ()
    {
        init (param1_specific_to_class_B, param2_specific_to_class_B); //calling parent class method 
        //do rest of teh random things
    }
}

Now, I am trying to write a JUnit test for class A. Basically I want to test whatever init method is doing is accurate or not. I tried this

Class clas = A.class;
B b = new B();

Method A_init;
A_init = clas.getDeclaredMethod("init", String.class, String.class);
A_init.invoke(b, param1_specific_to_class_B, param2_specific_to_class_B);

But its not working and i am getting following exception

java.lang.IllegalAccessException: Class test.package.subpackage.ATest can not access a member of class package.subpackage.A with modifiers "protected"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
    at java.lang.reflect.Method.invoke(Method.java:578)
    at test.package.subpackage.ATest.initTest(ATest.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)

Solution

  • You can place your test class in the same package as the class being tested. So the test class will have access to the protected method.

    Another option, a bit more elegant, is make the protected method accessible:

    A_init = clas.getDeclaredMethod("init", String.class, String.class);
    A_init.setAccessible(true);
    A_init.invoke(b, param1_specific_to_class_B, param2_specific_to_class_B);
    

    Hope it helps.