如何在通用 'Exception' catch 块的 Spock 单元测试中实现 jacoco 代码覆盖率?
How to implement jacoco code coverage in Spock unit testing for generic 'Exception' catch block?
我有一个 springboot rest api,它在基于类型 'String' 的 'studentId' 的数据库中执行 'Select' 操作。
下面我用一个学生示例模拟了这个场景。
我已经编写了 2 个单元测试(使用 spock 和 junit mockito)来验证功能。 (我在这个问题中没有提到单元测试代码,希望不需要它)
单元测试--
使用数据库中存在记录的有效 'studentId' 调用 api
此单元测试验证在 try {} 块(else 部分)
中编写的功能
使用无效的学生 ID 调用 api。数据库中没有该学号的记录。
此单元测试验证在 try {} 块(如果条件)中编写的功能,然后进入 catch(NoDataFoundException) 块
我还实现了 jacoco 来测量代码覆盖率。问题是 jacoco 覆盖了整个代码并将其标记为绿色,除了 catch(Exception) 块。
原因很明显,我没有提供任何单元测试来覆盖 catch(Exception) 块。
问题是我想不出任何可能导致 'Exception' 的情况。我仍然想在我的代码中保留 catch(Exception) 以便捕获任何不可预测的异常
有些人可能会争辩说我不应该有 catch(Exception),他们的推理可能是正确的,但它很重要,至少我正在从事的项目的性质确实如此。
有人遇到过这样的情况吗?您在 jacoco 代码覆盖率报告中为覆盖 catch(Exception) 所做的工作。任何建议,指针将不胜感激。
下面是控制器class-
@RestController
public class ApiController {
@Autowired
StudentInfoRepository studentInfo; // StudentInfoRepository is Interface having method 'fetchStudentDetails'. This method is provided implementation in a class named 'StudentInfoImplementation'
@RequestMapping(value = "/students/{studentId}", produces = "application/json", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Object> getStudentInfo(@PathVariable("studentId") String studentId) throws Exception {
Result<StudentModelClass> studentList = null;
ResponseEntity<Object> response = null;
try {
studentList = studentInfo.fetchStudentDetails(studentId);
//if no records are returned for given studentId then throw custom 'NoDataFoundException'
if (studentList.isExhausted()) {
throw new NoDataFoundException("No record found for the requested studentId: " + studentid);
//if records are returned for the given studentId then return 200 response and list of records
} else {
response = new ResponseEntity<Object>(studentList.all(), HttpStatus.OK);
return response;
}
}
catch(NoDataFoundException ex){
throw new NoDataFoundException(ex.getMessage());
}
catch(Exception ex){
throw new Exception(ex);
}
}
}
这是 Jacoco 的限制,如 FAQ
中所述
Source code lines with exceptions show no coverage. Why?
JaCoCo determines code execution with so called probes. Probes are
inserted into the control flow at certain positions. Code is
considered as executed when a subsequent probe has been executed. In
case of exceptions such a sequence of instructions is aborted
somewhere in the middle and the corresponding line of source code is
not marked as covered.
我有一个 springboot rest api,它在基于类型 'String' 的 'studentId' 的数据库中执行 'Select' 操作。 下面我用一个学生示例模拟了这个场景。
我已经编写了 2 个单元测试(使用 spock 和 junit mockito)来验证功能。 (我在这个问题中没有提到单元测试代码,希望不需要它)
单元测试--
使用数据库中存在记录的有效 'studentId' 调用 api 此单元测试验证在 try {} 块(else 部分)
中编写的功能
使用无效的学生 ID 调用 api。数据库中没有该学号的记录。 此单元测试验证在 try {} 块(如果条件)中编写的功能,然后进入 catch(NoDataFoundException) 块
我还实现了 jacoco 来测量代码覆盖率。问题是 jacoco 覆盖了整个代码并将其标记为绿色,除了 catch(Exception) 块。 原因很明显,我没有提供任何单元测试来覆盖 catch(Exception) 块。 问题是我想不出任何可能导致 'Exception' 的情况。我仍然想在我的代码中保留 catch(Exception) 以便捕获任何不可预测的异常
有些人可能会争辩说我不应该有 catch(Exception),他们的推理可能是正确的,但它很重要,至少我正在从事的项目的性质确实如此。
有人遇到过这样的情况吗?您在 jacoco 代码覆盖率报告中为覆盖 catch(Exception) 所做的工作。任何建议,指针将不胜感激。
下面是控制器class-
@RestController
public class ApiController {
@Autowired
StudentInfoRepository studentInfo; // StudentInfoRepository is Interface having method 'fetchStudentDetails'. This method is provided implementation in a class named 'StudentInfoImplementation'
@RequestMapping(value = "/students/{studentId}", produces = "application/json", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Object> getStudentInfo(@PathVariable("studentId") String studentId) throws Exception {
Result<StudentModelClass> studentList = null;
ResponseEntity<Object> response = null;
try {
studentList = studentInfo.fetchStudentDetails(studentId);
//if no records are returned for given studentId then throw custom 'NoDataFoundException'
if (studentList.isExhausted()) {
throw new NoDataFoundException("No record found for the requested studentId: " + studentid);
//if records are returned for the given studentId then return 200 response and list of records
} else {
response = new ResponseEntity<Object>(studentList.all(), HttpStatus.OK);
return response;
}
}
catch(NoDataFoundException ex){
throw new NoDataFoundException(ex.getMessage());
}
catch(Exception ex){
throw new Exception(ex);
}
}
}
这是 Jacoco 的限制,如 FAQ
中所述Source code lines with exceptions show no coverage. Why?
JaCoCo determines code execution with so called probes. Probes are inserted into the control flow at certain positions. Code is considered as executed when a subsequent probe has been executed. In case of exceptions such a sequence of instructions is aborted somewhere in the middle and the corresponding line of source code is not marked as covered.