Chinaunix首页 | 论坛 | 博客
  • 博客访问: 176755
  • 博文数量: 89
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 828
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 10:44
文章分类
文章存档

2014年(9)

2013年(80)

我的朋友

分类: Java

2013-10-17 17:40:46

我们知道,测试驱动开发(TDD)的基本思想就是在开发功能代码之前,先编写测试代码。也就是说在明确要开发某个功能后,首先思考如何对这个功能进行测试,并完成测试代码的编写,然后编写相关的代码满足这些测试用例。再循环进行添加其他功能,直到完全部功能的开发
最近,在公司一个支付系统的接口开发中,我按照上述方法进行了一次TDD的开发尝试。具体实现的功能是:解析渠道商返回的业务参数。首先,我整理出了TODO列表如下,
TODOs:
  1)参数完整与格式校验
  2)验证签名
  3)参数正确性校验
  4)通知状态校验
  5)正确解析参数与返回
然后,开始迭代式的演进开发,
?循环一(参数完整与格式校验):
1、编写测试用例
  1)参数完全正确用例
[java] view plaincopy
@Test  
public void parseNotifyParamsOk() {  
  
    Map notifyMap = new HashMap();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0");  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    notifyMap.put("verifystring", "123445");  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
    } catch (PartnerException pe) {  
        //仅当没有任何异常时,用例通过,否则认为用例失败  
        Assert.fail();  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  




    2)部分参数错误用例
[java] view plaincopy
@Test  
public void parseNotifyParamsError() {  
 
    Map notifyMap = new HashMap();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0aaaaaaaaaaaaaaaaaaaaaaaa");//此处参数格式不正确  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    notifyMap.put("verifystring", "123445");  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
        Assert.fail();  
  
    } catch (PartnerException pe) {  
        //仅当抛出参数错误异常时,用例通过,否则认为用例失败  
        if (!pe.getCode().equals(ErrorCode.PARAMETER_ERROR)) {  
            Assert.fail();  
        }  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  
2、运行测试用例,让测试失败
3、编写代码,让测试通过
[java] view plaincopy
@Override  
public NotifyMsg parseNotify(Map notifyMap, PartnerInfo info) throws Exception {  
  
    String orderId = notifyMap.get("order_id");  
    String result = notifyMap.get("result");  
    String amount = notifyMap.get("amount");  
      
    if(StringUtil.isBlank(orderId) || orderId.length() > 20){  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
      
    if(StringUtil.isBlank(result) || result.length() != 1 ){  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
      
    if(StringUtil.isBlank(amount) || !StringUtil.isNum(amount)){  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
    /**  
    *   其它参数逻辑类似  
    /  
  
}  
4、重构代码,去除重复
[java] view plaincopy
@Override  
public NotifyMsg parseNotify(Map notifyMap, PartnerInfo info) throws Exception {  
  
    ParamVerify pv = new ParamVerify();  
    pv.checkString("order_id",20,-1,true);//校验map中order_id的长度为20~无穷大,且为必填项  
    pv.checkString("result",1,1,true);//校验map中result的长度为1,且为必填项  
    pv.checkNum("amount",true);//校验map中amount为数字且必填  
      
    if( !pv.verify() ){  
        //如果检验不通过,则认为是请求参数不正确  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
    /**  
    *   其它参数逻辑类似  
    /  
  
}  


循环二(验证签名):
1、编写测试用例
1)签名正确的情况
[java] view plaincopy
@Test  
public void parseNotifySignOk() {  
  
    Map notifyMap = new HashMap();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0");  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    //签名正确的情况  
    notifyMap.put("verifystring", MD5Signature.sign(getSignSrc(notifyMap), md5Key, false));  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
    } catch (PartnerException pe) {  
        //仅当没有任何异常时,用例通过,否则认为用例失败  
        Assert.fail();  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  
2)签名错误的情况
[java] view plaincopy
@Test  
public void parseNotifySignError() {  
  
    Map notifyMap = new HashMap();  
    //我们验证的部分  
    notifyMap.put("order_id", "123456789");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "10");  
  
    notifyMap.put("version_id", "2.0");  
    notifyMap.put("order_date", "20100512");  
    notifyMap.put("result", "Y");  
    notifyMap.put("amount", "1");  
    notifyMap.put("currency", "rmb");  
    notifyMap.put("pay_sq", "123456789123456789");  
    notifyMap.put("pay_date", "20100512105835");  
    notifyMap.put("count", "12");  
    notifyMap.put("card_num1", "12345678912345678911");  
    notifyMap.put("card_pwd1", "12345678912345678922");  
    notifyMap.put("pm_id1", "01");  
    notifyMap.put("pc_id1", "2031");  
    notifyMap.put("card_status1", "0");  
    notifyMap.put("card_code1", "00000");  
    notifyMap.put("card_date1", "20100512105835");  
    notifyMap.put("r1", "2");  
  
    String md5Key = "abcde";  
    PartnerInfo info = new PartnerInfo();  
    info.setMd5Key(md5Key);  
  
    notifyMap.put("verifystring", "123445");  
  
    try {  
        //解析请求参数的核心方法  
        NotifyMsg notify = partnerService.parseNotify(notifyMap, info);  
        Assert.fail();  
  
    } catch (PartnerException pe) {  
        //仅当抛出签名错误异常时,用例通过,否则认为用例失败  
        if (!pe.getCode().equals(ErrorCode.PARAMETER_ERROR)) {  
            Assert.fail();  
        }  
    } catch (Exception e) {  
        Assert.fail();  
        e.printStackTrace();  
    }  
}  


2、运行测试用例,让测试失败
3、编写代码,让测试通过
[java] view plaincopy
@Override  
public NotifyMsg parseNotify(Map notifyMap, PartnerInfo info) throws Exception {  
  
    ParamVerify pv = new ParamVerify();  
    pv.checkString("order_id",20,-1,true);//校验map中order_id的长度为20~无穷大,且为必填项  
    pv.checkString("result",1,1,true);//校验map中result的长度为1,且为必填项  
    pv.checkNum("amount",true);//校验map中amount为数字且必填  
      
    if( !pv.verify() ){  
        //如果检验不通过,则认为是请求参数不正确  
        throw new PartnerException(ErrorCode.PARAMETER_ERROR, "渠道返回数据格式不正确");  
    }  
    /**  
    *   其它参数逻辑类似  
    /  
    //验证签名  
    if (!MD5Signature.verify(getSignSrc(notifyMap), info.getMd5Key(), notifyMap.get("verifystring"), true)) {  
        LOGGER.error("渠道返回接口通知参数验签失败");  
        throw new PartnerException(ErrorCode.SIGN_ERROR, "渠道返回数据签名错误");  
    }  
  
}  
4、重构(因为结构简单,无需重构,跳过此步)


按上述步骤循环进行,直至完成全部功能,就是一个简单的测试驱动开发的实战例子了。经过这样过程产生的代码,具有大量的测试用例,因此具备相对好的健壮性、可扩展性。
阅读(1743) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~