junit私有方法测试

字体大小: 中小 标准 ->行高大小: 标准
    很多人说类的私有方法不用测试,理由是类私有方法只允许被本类访问,而其他类无权调用。事实上并非如此,来看一个范例程序,Commodity_Parent是一个商品父类,定义了两个商品类基础属性:commodity_name,commodity_price,分别是商品的名称和价格。二组公共和两组受保护的实例方法分别是getcommodity_name(),getcommodity_price,setcommodity_name(),setcommodity_price(),对应的功能是取商品名称、价格,设置商品名称、价格。具体代码如下:
 
package com.fastpoint.Private;
 
 
 
public class Commodity_Parent {
 
       private String commodity_name;
 
       private Double commodity_price;
 
 
 
       public Commodity_Parent(String name, Double price) {
 
              commodity_name = name;
 
              commodity_price = price;
 
       }
 
 
 
       public String getCommodity_name() {
 
              return commodity_name;
 
       }
 
 
 
       public void setCommodity_name(String commodity_name) {
 
              this.commodity_name = commodity_name;
 
       }
 
 
 
       public Double getCommodity_price() {
 
              return commodity_price;
 
       }
 
 
 
       public void setCommodity_price(Double commodity_price) {
 
              this.commodity_price = commodity_price;
 
       }
 
}
 
        Commodity_Child类继承了Commodity_Parent类,并且增加了一个新的类属性Commodity_number,该属性用来记录具体商品类的计量数量。GetCommodityNumber()方法用来取得该类实例的商品数量,对应的SetCommodityNumber()实例方法被声明为私有的,从这里可以看出,程序不允许别人使用该方法对商品数量做直接的修改,而是通过一个共有的setData()方法实现更安全的商品数量操作,具体代码如下:
 
package com.fastpoint.Private;
 
 
 
public class Commodity_Child extends Commodity_Parent {
 
       private Integer Commodity_number;
 
 
 
       public Commodity_Child(String Commodity_name, double Commodity_price,
 
                     Integer number) {
 
              super(Commodity_name, Commodity_price);
 
              Commodity_number = number;
 
       }
 
 
 
       public Integer getCommodity_number() {
 
              return Commodity_number;
 
       }
 
 
 
       private void setCommodity_number(Integer commodity_number) {
 
              Commodity_number = commodity_number;
 
       }
 
 
 
       public void setData(Integer commoditynumber) {
 
              if (isValidate()) {
 
                     if (isCommodityNumber(commoditynumber)) {
 
                            setCommodity_number(commoditynumber);
 
                     } else {
 
                            System.out.println("没有足够的商品数量!");
 
                     }
 
              } else {
 
                     System.out.println("没有足够权限!");
 
              }
 
       }
 
 
 
       public boolean isValidate() {
 
              //示例程序 简化部分代码
 
              return true;
 
       }
 
       public boolean isCommodityNumber(double d) {
 
              //示例程序 简化部分代码
 
              return true;
 
       }
 
}
 
    可以利用java的反射机制来实现访问类的私有方法或者属性。
 
    使用java.lang.Class的相关方法,获得相关指定对象的Field,然后调用field.setAccessible(true);绕过访问权限的检查,然后可以访问Field的值,当然也可以设置Field的值。
 
    使用java.lang.Class的相关方法,获得相关指定对象的Method;然后调用field.setAccessible(true);绕过访问权限的检查;最后执行该方法.
 
测试代码如下所示:
 
package com.fastpoint.testPrivate;
 
 
 
import static org.junit.Assert.*;
 
 
 
import java.lang.reflect.InvocationTargetException;
 
import java.lang.reflect.Method;
 
 
 
import org.junit.After;
 
import org.junit.Before;
 
import org.junit.Test;
 
 
 
public class Commodity_ChildTest {
 
 
 
       @Before
 
       public void setUp() throws Exception {
 
       }
 
 
 
       @After
 
       public void tearDown() throws Exception {
 
       }
 
 
 
       @Test
 
       public void testSetCommodity_number() throws ClassNotFoundException,
 
                     SecurityException, NoSuchMethodException, IllegalArgumentException,
 
                     IllegalAccessException, InvocationTargetException,
 
                     InstantiationException {
 
              Commodity_Child comm = new Commodity_Child();
 
              Class[] parameterTypes = new Class[1];// 这里你要调用的方法只有一个参数
 
              parameterTypes[0] = Integer.class;// 参数类型为String[]
 
              Method method = comm.getClass().getDeclaredMethod(
 
                            "setCommodity_number", Integer.class);// 要调用的方法是SetCommodity_number
 
              Object[] args = new Object[1];
 
              method.setAccessible(true);// 允许处理私有方法
 
              method.invoke(comm, new Object[] { new Integer(15) });// 调用方法
 
              method.setAccessible(false);
 
              assertEquals(15, comm.getCommodity_number(), 0);
 
       }
 
 
 
}
 
上面代码演示了测试Commodity_Child类中的私有方法setCommodity_Number(),主要运用了Java的反射机制。

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/69373.html