Re: unit testing private methods



On Aug 13, 4:00 am, jimgardener <jimgarde...@xxxxxxxxx> wrote:
hi
i am trying out PrivilegedAccessor class (of  http://sourceforge.net/projects/privaccessor/)
along with junit to test some private methods that process double[]
[] .I think this uses reflection to access private methods

public class MyClass {
    private double[] processArray(double[] inarray){
        double[] ret=new double[inarray.length];
        for(int i=0;i<inarray.length;i++){
                ret[i]=inarray[i]+100.0;
        }
        return ret;
    }

}

here is the testcase class
<code>
import junit.framework.TestCase;
import junit.extensions.PrivilegedAccessor;

public class MyClassTest extends TestCase{
        public MyClassTest(String name){
                super(name);
        }
        public void testMyClass()throws Exception{
                MyClass mc=new MyClass();
                assertNotNull(mc);

                double[] inputarray=new double[]{1.1,2.2,3.3,4.4};
                double[] ans=new double[]{101.1,102.2,103.3,104.4};
                double[] outputarray=(double[])
(PrivilegedAccessor.invokeMethod(mc,"processArray(double[])",inputarray));
                assertEquals(outputarray,ans);

       }

}

<code>

when i run the test ,i get an error message like

java.lang.NoSuchMethodException: Method 'processArray(double[])'s
parameter nr1 (double[]) not found

This error originates at the call PrivilegedAccessor.invokeMethod(..)
can someone tell me why this happens?

Private methods are amenable to proving via assertions. It is not
usual to unit-test private methods, at least not with mechanisms
(reflection) that are more bug-prone than the code under test.
Private methods, being under total control of their owning class,
depend on and establish program invariants. Put assertions at the
invariant points to prove them. This would render all that
complicated reflection fooferol unnecessary for unit-test purposes.

It is still valid, of course, to use reflection in order to learn how
to use reflection, but its use in production code should be limited to
those use cases where it helps more than it hurts. I particularly
recommend against unit-test code that introduces dependencies (other
than on the unit-test framework itself) that do not exist in the code
under test.

--
Lew
.



Relevant Pages

  • Re: JUnit FAQ suggests private methods bad design?
    ... I don't know if reflection in Java has this ... The easiest method I've found to test private methods is to make the ... A static inner class has access to all ... Another benefit to using inner classes is that they force the developer ...
    (comp.object)
  • Calling an internal constructor
    ... I know I can use reflection to call internal, protected, and private methods ... I'm so mad at MS about the CurrencyManager design it makes me want to ... derived from BindingManagerBase, and though the documentation has a "Note to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: NUnit Test
    ... Not an easy way, look at reflection and you can get hold of private methods, ... but a bit of extra work to get there. ... I'm new to NUnit. ...
    (microsoft.public.dotnet.languages.csharp)