How to test the Spring Boot services Catch Block using Junit 5?

Viewed 2413

In my business logic I don't have any scenario for exception, Not sure how to test the catch block in sprint boot Service class. Most of the scenarios are handled in controller level.

But for sonar coverage I need to cover catch block too in my services.

My sample code be like

public void employeeDetails(Object Employee) throws CustomException {
        try {
            int count = commonUtils.calculateEmployeeCount(Employee)
        } catch (Exception e) {
            throw new CustomException(" Exception occurred", e);
        }
    } 

Whether my business logic should have any failure scenarios to test or we have any option to mock custom virtual exception for testing purpose. Any one please advice on this.

Reference

How do you assert that a certain exception is thrown in JUnit 4 tests?

How to Junit test a try catch block in my code

1 Answers

I assume a class EmployeeServiceImpl implementing EmployeeService with the method void EmployeeDetails(Object Employee). Note, according to the code conventions, the names of methods start with lower-case, so I will follow such convention in my answer.

public void employeeDetails(Object employee) throws CustomException {
    try {
        int count = calculateEmployeeCount(employee)
    } catch (Exception e) {
        throw new CustomException("Exception occurred", e);
    }
} 

You need to achieve calculateEmployeeCount method to throw an exception, which can be any subtype of Exception. For that you need instance of the class having such method. The instance should be either mocked using @MockBean or if it's our EmployeeServiceImpl, you use @SpyBean for partial mocking instead of @Autowired for a production bean with injected beans with precedence of the test ones (@MockBean) over the production ones.

Assuming you use Spring tests starter with Mockito and the same class having calculateEmployeeCount method (judging from its name).

@SpyBean
private EmployeeService employeeService;
Mockito.when(employeeService.calculateEmployeeCount())
       .thenThrow(new Exception("An exception"));

The final step is a verification of whether the CustomException was thrown:

Object employee = new Object();                        // it doesn't need to be a mock

CustomException exception = Assertions.assertThrows(
     CustomException.class,
     () -> employeeService.employeeDetails(employee));

// the thrown exception is available fur further testing (of the message, cause etc.)
Related