Rest 如何在Spock单元测试中实现jacoco代码覆盖率,用于泛型';例外情况';挡块?
我有一个springboot rest api,它在数据库中基于类型为“String”的“studentId”执行“Select”操作。 下面我用一个学生示例模拟了这个场景 我已经编写了两个单元测试(使用spock和junit mockito)来验证功能。(我在这个问题中没有提到单元测试代码,希望它不是必需的) 单元测试--Rest 如何在Spock单元测试中实现jacoco代码覆盖率,用于泛型';例外情况';挡块?,rest,api,junit,spock,jacoco,Rest,Api,Junit,Spock,Jacoco,我有一个springboot rest api,它在数据库中基于类型为“String”的“studentId”执行“Select”操作。 下面我用一个学生示例模拟了这个场景 我已经编写了两个单元测试(使用spock和junit mockito)来验证功能。(我在这个问题中没有提到单元测试代码,希望它不是必需的) 单元测试-- 使用数据库中存在记录的有效“studentId”调用api 此单元测试验证在try{}块(else部分)中编写的功能 使用无效的学生id调用api。数据库中没有此学生id的
@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);
}
}
@RestController
公共类ApiController{
@自动连线
StudentInfoRepository studentInfo;//StudentInfoRepository是具有方法“fetchStudentDetails”的接口。此方法是在名为“StudentInfoImplementation”的类中提供的实现
@RequestMapping(value=“/students/{studentId}”,products=“application/json”,method=RequestMethod.GET)
@应答器
公共响应getStudentInfo(@PathVariable(“studentId”)字符串studentId)引发异常{
结果studentList=null;
ResponseEntity response=null;
试一试{
studentList=studentInfo.fetchStudentDetails(studentId);
//如果没有为给定的studentId返回任何记录,则抛出自定义的“NoDataFoundException”
if(studentList.isexhousted()){
抛出新的NoDataFoundException(“未找到请求的studentId:+studentId的记录”);
//如果返回给定studentId的记录,则返回200个响应和记录列表
}否则{
response=newresponseEntity(studentList.all(),HttpStatus.OK);
返回响应;
}
}
捕获(NoDataFoundException-ex){
抛出新的NoDataFoundException(例如getMessage());
}
捕获(例外情况除外){
抛出新异常(ex);
}
}
}这是Jacoco的一个限制,如 有异常的源代码行不显示覆盖范围。为什么? JaCoCo使用所谓的探测来确定代码的执行。探针是 在某些位置插入控制流。代码是 执行后续探测时视为已执行。在里面 在异常情况下,该指令序列被中止 中间的某个地方和对应的源代码行是 未标记为覆盖的
这是Jacoco的一个限制,如 有异常的源代码行不显示覆盖范围。为什么? JaCoCo使用所谓的探测来确定代码的执行。探针是 在某些位置插入控制流。代码是 执行后续探测时视为已执行。在里面 在异常情况下,该指令序列被中止 中间的某个地方和对应的源代码行是 未标记为覆盖的
谢谢莱纳德的回答!现在,似乎并没有别的办法,只能在代码覆盖率较低的情况下生活。不管怎样,谢谢你指出这是jacoco的问题,否则我必须挖掘更多的信息来找到一个有效的解决方案。是的,我知道这也让我感到困扰,但这就是他们实施它的方式@黑手党你能接受这个答案吗?谢谢莱纳德的回答!现在,似乎并没有别的办法,只能在代码覆盖率较低的情况下生活。不管怎样,谢谢你指出这是jacoco的问题,否则我必须挖掘更多的信息来找到一个有效的解决方案。是的,我知道这也让我感到困扰,但这就是他们实施它的方式@黑手党你能接受这个答案吗?