Model view controller 如何提高cobertura评分

Model view controller 如何提高cobertura评分,model-view-controller,spring,Model View Controller,Spring,朋友们好,我是新来的,请不要介意我问一个很简单的问题 我正在研究Spring3.0,我正在使用cobertura检查我的代码覆盖率 虽然我所有的测试用例都是成功的,但由于错误块的原因,我无法得到完全覆盖 谁能帮我盖上红色的积木吗 提前谢谢 //This Code is For SQL Map of DAO @SuppressWarnings(ApplicationConstants.UNCHECKED) @Override public List<ReportListDTO>

朋友们好,我是新来的,请不要介意我问一个很简单的问题

我正在研究Spring3.0,我正在使用cobertura检查我的代码覆盖率

虽然我所有的测试用例都是成功的,但由于错误块的原因,我无法得到完全覆盖

谁能帮我盖上红色的积木吗

提前谢谢

//This Code is For SQL Map of DAO

@SuppressWarnings(ApplicationConstants.UNCHECKED) 
@Override 
public List<ReportListDTO> searchGeneralReport(
        final ReportSearch reportSearch) throws COBException { 
    List<ReportListDTO> generalSearchList; 
    try{ 
        generalSearchList = this.sqlMapClientTemplate.queryForList(
                "cobSQLReportQuery.searchGeneralReport",reportSearch); 
    }catch(DataAccessException dae){ 
        // NOT COVERED BY COBERTURA
        throw new COBException(this.getClass().getName() 
                + "SQL Exception in COB DB Operation",dae);
    } 
    LOGGER.info("TableSize Coming From DB"+generalSearchList.size()); 
    return generalSearchList; 
} 
//此代码用于DAO的SQL映射
@抑制警告(ApplicationConstants.UNCHECKED)
@凌驾
公共列表搜索一般报告(
最终报告搜索(ReportSearch)引发异常{
列出通用搜索列表;
试试{
generalSearchList=this.sqlMapClientTemplate.queryForList(
“cobSQLReportQuery.searchGeneralReport”,reportSearch);
}捕获(DataAccessException dae){
//不在COBERTURA保险范围内
抛出新的COBException(this.getClass().getName())
+“COB数据库操作中的SQL异常”,dae);
} 
info(“来自DB的表大小”+generalSearchList.size());
返回通用搜索列表;
} 
有人能帮我获得突出显示行的代码覆盖率吗

//This Code For Controller
@RequestMapping(method = RequestMethod.GET, value = ApplicationConstants.GENERAL_REPORT_SEARCH) 
public ModelAndView searchGeneralReport(
        final @ModelAttribute(ApplicationConstants.GENERAL_REPORT_COMMAND) ReportSearch reportSearch, 
        final BindingResult result) throws COBException { 

    final String[] mainSearchG = StringUtils.split(ApplicationConstants.REPORT_SEARCH_CRITERIA,","); 
    final String[] mainSearchV = StringUtils.split(ApplicationConstants.REPORT_SEARCH_CRITERIA_VALUES,","); 
    final String[] daysOutstanding= StringUtils.split(ApplicationConstants.REPORT_DAYS_OUTSTANDING,","); 
    //Splitting LookUp's 
    final List<LookUpValueBean> lookupSearchG = getLookUpValList(mainSearchG,mainSearchV); 
    final List<LookUpValueBean> daysOutG= getLookUpList(daysOutstanding); 
    final ModelAndView mav = new ModelAndView(); 
    final ReportListDTO reportListDTO= new ReportListDTO(); 
    List<ReportListDTO> reportList= new ArrayList<ReportListDTO>(); 
    mav.addObject("lookupSearchG", lookupSearchG); 
    mav.addObject("daysOutG", daysOutG); 
    mav.addObject(ApplicationConstants.GENERAL_REPORT_COMMAND,reportListDTO); 
    mav.addObject(ApplicationConstants.REPORT_SEARCH_ADD,reportSearch); 
    try{ 
        validator.validate(reportSearch, result); 
        if(reportSearch.getMainSearch().equalsIgnoreCase("CREATED ON")&&!"".equals(reportSearch.getCreatedOnDate())){ 
            reportValidator.validate(reportSearch, result); 
        } 
        if(result.hasErrors()){ 
            // NOT COVERED BY COBERTURA
            mav.addObject("lookupSearchG", lookupSearchG); 
            mav.addObject("daysOutG", daysOutG); 
            mav.setViewName(ApplicationConstants.REPORT_GENERAL_VIEW);         
        } else{ 
            reportList = reportService.searchGeneralReport(reportSearch); 
            mav.addObject("reportList", reportList); 
        } 
    } catch(COBException e){ 
        // not covered
        LOGGER.error(e.getMessage(),e); 
    } 
    mav.setViewName(ApplicationConstants.REPORT_GENERAL_VIEW); 
    return mav; 
} 
//控制器的此代码
@RequestMapping(method=RequestMethod.GET,value=ApplicationConstants.GENERAL\u REPORT\u SEARCH)
公共模型和视图搜索通用报告(
final@modeldattribute(ApplicationConstants.GENERAL_REPORT_命令)ReportSearch ReportSearch,
最终BindingResult)引发异常{
最终字符串[]mainSearchG=StringUtils.split(ApplicationConstants.REPORT\u SEARCH\u CRITERIA,“,”;
最终字符串[]mainSearchV=StringUtils.split(ApplicationConstants.REPORT\u SEARCH\u CRITERIA\u VALUES,“,”;
最终字符串[]daysOutstanding=StringUtils.split(ApplicationConstants.REPORT_DAYS_未决,“,”;
//拆分查找的
最终列表lookupSearchG=getLookUpValList(mainSearchG,mainSearchV);
最终列表daysOutG=getLookUpList(daysOutstanding);
最终模型和视图mav=新模型和视图();
最终报告ListDTO ReportListDTO=新报告ListDTO();
List reportList=new ArrayList();
mav.addObject(“LookupSearch”,LookupSearch);
mav.addObject(“daysOutG”,daysOutG);
mav.addObject(ApplicationConstants.GENERAL\u REPORT\u命令,reportListDTO);
mav.addObject(ApplicationConstants.REPORT\u SEARCH\u ADD,reportSearch);
试试{
validator.validate(报告搜索、结果);
如果(reportSearch.getMainSearch().equalsIgnoreCase(“创建日期”)&&&&!“”.equals(reportSearch.getCreatedOnDate()){
reportValidator.validate(reportSearch,result);
} 
if(result.hasErrors()){
//不在COBERTURA保险范围内
mav.addObject(“LookupSearch”,LookupSearch);
mav.addObject(“daysOutG”,daysOutG);
mav.setViewName(ApplicationConstants.REPORT\u GENERAL\u VIEW);
}否则{
reportList=reportService.searchGeneralReport(reportSearch);
mav.addObject(“报告列表”,报告列表);
} 
}捕获(COBE){
//未涵盖
LOGGER.error(e.getMessage(),e);
} 
mav.setViewName(ApplicationConstants.REPORT\u GENERAL\u VIEW);
返回mav;
} 

基本上,您希望在单元测试中使用模拟来模拟这些异常情况

测试代码的第一位 在单元测试中,注入一个模拟的
sqlMapClientTemplate
,当调用
queryForList
时抛出
DataAccessException
。在这种情况下,您的测试应该检查
COBException

测试代码的第二位 两项新测试

  • 传入一个
    BindingResult
    ,该结果对于
    hasErrors()
    返回true,然后检查
    ModelAndView
    是否包含您期望的内容
  • 注入一个模拟的
    reportSearch
    ,生成
    COBException
    。不知道如何检查断言发生了什么,但您可以通过这种方式获得覆盖率

在我详细介绍之前,值得指出的是,实现100%的覆盖率并不是一个值得追求的目标。相反,您应该着眼于对重要代码的良好覆盖。对于发生不可见错误的可能性较低的代码段,覆盖率不太重要;e、 g.当代码非常简单,一次性目视检查就足够*时

(此外,100%的覆盖率本身并不能保证充分的测试。)


第一个例子稍微有用。看起来只有在SQL不正确或故意提供错误记录时才会引发异常。考虑到代码的性质,我会错过这个。(但如果您真的想这样做,您可以使用一个对象来模拟
sqlMapClientTemplate
,该对象会产生一些糟糕的SQL…例如。)

第二个例子值得一提。您应该能够通过传递未通过其中一个验证的
reportSearch
来触发它

第三个例子不值得一提。你可以通过检查判断代码是正确的。(但是注入第一个示例中的坏SQL应该可以工作。)


*这个论点有一个缺陷。有人可能会将一些未经测试的“明显正确”的行替换为其他不正确的行。所以我想应该有一个代码检查工具,可以用来“检查”未测试的代码部分。。。并注意自上次检查以来代码的更改时间