Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > unit testing private methods

Reply
Thread Tools

unit testing private methods

 
 
jimgardener
Guest
Posts: n/a
 
      08-13-2008
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?

thanks
jim
 
Reply With Quote
 
 
 
 
Andreas Kutschera
Guest
Posts: n/a
 
      08-13-2008
jimgardener 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>
> 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?


Looking at the source code, it looks like PrivilegedAccessor cannot handle
arrays.
When I run your example, it breaks because it cannot find a class by the
name of "double[]" (yes, the "[]" is part of the wanted class name).

So you probably have two choices now:
change your method signature or
fix PrivilegedAccessor
 
Reply With Quote
 
 
 
 
Simon
Guest
Posts: n/a
 
      08-13-2008
> (PrivilegedAccessor.invokeMethod(mc,"processArray( double[])",inputarray));

I can only guess, but if PriviledgedAccessor tries something like
Class.forName("double[]") it is not surprising that it fails. The correct string
for the array class in this case would be "[D". For Objects, the array class can
be obtained by using "[Lpackage.name.ClassName;".

Hope this helps,
Simon
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      08-13-2008
jimgardener 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?
>
> thanks
> jim

Unit tests are better off testing public interfaces only. Private
implementation details shouldn't break the test unless they break client
code. They also shouldn't break client code unless they break the test.

Now, if the private method is an implementation of a particular
algorithm that is used in several parts of the same class, you might
consider pulling it out into its own class, making the method public,
and unit testing *that* class. This gives you two benefits: The user of
your class can provide you with a different implementation of that
algorithm if they choose, and any other part of your system that needs
to use that algorithm has access to the new class.

Hope this helps,
Daniel.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-13-2008
On Aug 13, 4:00*am, jimgardener <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unit-profiling, similar to unit-testing Ulrich Eckhardt Python 6 11-18-2011 02:00 AM
Test::Unit - Ruby Unit Testing Framework Questions Bill Mosteller Ruby 0 10-22-2009 02:02 PM
Private methods not so private? Frank Meyer Ruby 14 08-02-2007 07:29 PM
Why is define_method private? Plus,what's the point of private methods? Daniel Finnie Ruby 3 12-16-2006 10:09 PM
Newbie: Unit testing a private method with arguments Marcus Lindemann Ruby 2 10-27-2005 02:49 PM



Advertisments