引用:http://blog.csdn.net/badwps/article/details/1629990
当Java的类有一个方法的访问修饰符是private,我们在写测试类时该怎样对它进行测试吗?道难是先将它的修饰符改成public,然后在做完测试之后再将其改回public,其实完全不用这样做,我们在编写测试类的时候只要将其私有方法抽出,让其可以访问,就可以按要求进行测试了.
开始我们先写一个被测试类,如下:
package
com.srcopen.test;
public class Example ... {
private int add(int a,int b)...{
return a+b;
}
public int doAdd(int c,int d)...{
return this.add(c, d);
}
}
public class Example ... {
private int add(int a,int b)...{
return a+b;
}
public int doAdd(int c,int d)...{
return this.add(c, d);
}
}
这个类非常的简单,它有一个相加的私有方法,进行a+b,然后在一个公有方法doAdd(int,int)中进行调用.下面让我们写一个简单的测试类对这个被测试类进行测试,如下:
package
com.srcopen.test;
import junit.framework. * ;
public class Example_Test extends TestCase ... {
public void testDoAdd() ...{
Example example=new Example();
assertEquals(4,example.doAdd(1,3));
}
}
import junit.framework. * ;
public class Example_Test extends TestCase ... {
public void testDoAdd() ...{
Example example=new Example();
assertEquals(4,example.doAdd(1,3));
}
}
这对被测类的公有方法进行测试,应该是没问题的,然后对其私有方法进行测试,我们要想办法将其私有方法通过编程变成私有方法,主要用去Method这个类,将我们要测试的方法赋给它,其中它有一个设置,可使方法变成可进入,这样就可以达到测试的目的了,编写的完整代码如下:
package
com.srcopen.test;
import junit.framework. * ;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class Example_Test extends TestCase ... {
public void testDoAdd() ...{
Example example=new Example();
assertEquals(4,example.doAdd(1,3));
}
public void testAdd() ...{
Example example=new Example();
Object o=null;
try...{
Method method = example.getClass().getDeclaredMethod("add",new Class[] ...{int.class, int.class});
method.setAccessible(true); //让其方法可访问
o = method.invoke(example, new Object[] ...{1, 3});
}catch(Exception e)...{
}
assertEquals("4",o.toString());
}
}
import junit.framework. * ;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class Example_Test extends TestCase ... {
public void testDoAdd() ...{
Example example=new Example();
assertEquals(4,example.doAdd(1,3));
}
public void testAdd() ...{
Example example=new Example();
Object o=null;
try...{
Method method = example.getClass().getDeclaredMethod("add",new Class[] ...{int.class, int.class});
method.setAccessible(true); //让其方法可访问
o = method.invoke(example, new Object[] ...{1, 3});
}catch(Exception e)...{
}
assertEquals("4",o.toString());
}
}
注意导入Method类的时候要为import java.lang.reflect.Method;
还要就是使用method.setAccessible(true)将这个方法设为可以访问.
运行一下我们发现已经可以进行测试了.~~
- package com.bill99.junit;
- public class ACase {
- private String echoRequest(String request) {
- return "Hello!"+request;
- }
- private String echoRequest() {
- return "Hello!";
- }
- }
package com.bill99.junit;
public class ACase {
private String echoRequest(String request) {
return "Hello!"+request;
}
private String echoRequest() {
return "Hello!";
}
}
- package com.bill99.junit;
- import java.lang.reflect.Method;
- import junit.framework.Assert;
- import org.junit.Before;
- import org.junit.Test;
- public class ACaseTest {
- ACase a =null;
- @Before
- public void setUp() throws Exception {
- a = new ACase();
- }
- @Test
- public void testNoParamEchoRequest() throws Exception {
- //测试没有参数的echoRequest()方法
- Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest", null);
- //Method对象继承自java.lang.reflect.AccessibleObject,父类方法setAccessible可调
- //将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。
- //要访问私有方法必须将accessible设置为true,否则抛java.lang.IllegalAccessException
- testNoParamMethod.setAccessible(true);
- //调用
- Object result = testNoParamMethod.invoke(a, null);
- System.out.println(result);
- Assert.assertNotNull(result);
- }
- @Test
- public void testParamEchoRequest() throws Exception {
- //测试带有参数的echoRequest(String request)方法
- Method testNoParamMethod = a.getClass().getDeclaredMethod("echoRequest",String.class);
- testNoParamMethod.setAccessible(true);
- //调用
- Object result = testNoParamMethod.invoke(a, "this is a test information");
- System.out.println(result);
- Assert.assertNotNull(result);
- }
- }