I'm quite new to Junit and JaCoCo. I'm trying to add test case for the catch block. But my JaCoCo code coverage is still asking me to cover the catch block in code coverage. Following is my method and testcase.
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);
}
Please share me the correct way for the code coverage of catch block.
Note: JaCoCo version: 0.8.5, Junit version; junit5, Java version: 11