如何在代码覆盖率中包含 Catch 块:JaCoCo 和 Junit

问题描述

我对 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);
}

enter image description here

请分享catch块代码覆盖的正确方法。

注:JaCoCo 版本:0.8.5,Junit 版本; junit5,Java 版本:11

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)