0

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

PowerMockito with Jacoco Code Coverage

https://www.igorkromin.net/index.php/2018/02/20/jacoco-reports-missing-code-coverage-for-tests-using-powermock/

https://github.com/mockito/mockito/issues/969

agabrys
  • 8,728
  • 3
  • 35
  • 73
Debugger
  • 690
  • 1
  • 18
  • 41

1 Answers1

0

For my understanding the employeeService object keeps a mock:

I have mocked by spring boot service

It means that your test doesn't test anything. Why? The test code interacts only with the employeeService object. It means that the real service implementation is not executed (not tested at all). You have to use a spy if you want to check only part of the service:

@Test
public void employeedetails() throws CustomException {
    EmployeeService employeeService = Mockito.spy(new RealEmployeeServiceImpl(...));
    Employee employee = new Employee();
    Mockito.doThrow(new CustomException("Exception while getting the employee details"))
           .when(employeeService).getEmployeeDetails(employee)

    CustomException exception = Assertions.assertThrows(
            CustomException.class,
            () -> employeeService.getEmployeeDetails(caution));

    // rest of the test
}

What is very important, the construction

Mockito.when(employeeService.getEmployeeDetails(employee))
       .thenThrow(new CustomException("Exception while getting the employee details"));

has been changed to

Mockito.doThrow(new CustomException("Exception while getting the employee details"))
       .when(employeeService).getEmployeeDetails(employee);

The employeeService object is not a mock, but an instance of the employee service class. The first structure is calling the getEmployeeDetails method. The second doesn't do it. The method is called on Mockito "configuration object instead of on the employeeService object (no direct execution = no real method call).

agabrys
  • 8,728
  • 3
  • 35
  • 73