zoukankan      html  css  js  c++  java
  • 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.cnblogs.com/Automation_software/ 只求在IT界有一个清闲的世界让我静心的去专研,不求功名利禄,只为心中的那份成就感及自我成长、自我实现的快感。
  • 相关阅读:
    nopcommerce商城系统--源代码结构和架构
    nopcommerce商城系统--如何编写一个插件
    ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
    【js与jquery】电子邮箱、手机号、邮政编码的正则验证
    也用 Log4Net 之走进Log4Net (四)
    也用 Log4Net 之将自定义属性记录到文件中 (三)
    也用 Log4Net 之将日志记录到数据库的后台实现 (二)
    也用 Log4Net 之将日志记录到数据库的配置 (一)
    log4net 将日志写入数据库
    ASP.NET MVC 3 入门级常用设置、技巧和报错
  • 原文地址:https://www.cnblogs.com/Automation_software/p/1945131.html
Copyright © 2011-2022 走看看