【发布时间】:2021-02-12 10:08:30
【问题描述】:
我对 Junit 和 JaCoCo 很陌生。我正在尝试为 catch 块添加测试用例。但是我的 JaCoCo 代码覆盖率仍然要求我覆盖代码覆盖率中的 catch 块。 以下是我的方法和测试用例。
public Student addStudent(Student Stu) throws CustomException {
try {
// My Business Logic
return Student;
} catch (Exception e) {
throw new CustomException("Exception while Adding Student ", e);
}
}
@SneakyThrows
@Test
public void cautionRunTimeException(){
when(studentService.addStudent(student)).thenThrow(RuntimeException.class);
assertThrows(RuntimeException.class,()-> studentService.addStudent(student));
verify(studentService).addStudent(student);
}
请分享一下catch块代码覆盖的正确方法。
注意:JaCoCo 版本:0.8.5,Junit 版本; junit5,Java 版本:11
【问题讨论】:
标签: java unit-testing junit jacoco