I have mocked by spring boot service class for testing the catch block statements. My sample test case is like:
@SpyBean
private EmployeeService employeeService;
@Test
public void employeedetails() throws CustomException {
Employee employee= new Employee();
Mockito.when(employeeService .getEmployeeDetails(employee))
.thenThrow(new CustomException("Exception while getting the employee details"));
CustomException exception = Assertions.assertThrows(
CustomException.class,
() -> employeeService.getEmployeeDetails(caution));
}
POM:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>aggregate-coverage-reports</id>
<phase>test</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Test case are executed fine but it's not reflected in the Java code coverage report. Still my catch statements are showing its not covered by test.
What could be the reason?
Reference