0

I have a simple Rest Controller as below

 @RestController
    public class HealthController {
    
  private static final CustomLogger logger = CustomLogger.getLogger(HealthController.class.getName());
    
      private HealthService healthService;
    
      @Autowired
      public HealthController(HealthService healthService) {
        this.healthService = healthService;
      }
    
      @RequestMapping(value = "/health", method = RequestMethod.GET)
      public ResponseEntity<?> healthCheck() {
           return healthService.checkHealth();
      }
    
    
    }

The service class is below

@Service
public class HealthService {


 private static final CustomLogger logger = CustomLogger.getLogger(HealthController.class.getName());

  public ResponseEntity<?> checkHealth() {
    logger.info("Inside Health");
    if (validateHealth()) {
      return new ResponseEntity<>("Healthy", HttpStatus.OK);
    } else {
      return new ResponseEntity<>("Un Healthy", HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  boolean validateHealth() {
      return true;
  }


}

The corresponding unit test for the controller class as below

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = HealthController.class)
public class HealthControllerTest {


  @Autowired
  private MockMvc mockMvc;



  @MockBean
  private HealthService healthService;



  @Test
  public void checkHealthReturn200WhenHealthy() throws Exception {
    ResponseEntity mockSuccessResponse = new ResponseEntity("Healthy", HttpStatus.OK);
    when(healthService.checkHealth()).thenReturn(mockSuccessResponse);
    RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
        "/health").accept(
        MediaType.APPLICATION_JSON);
    MvcResult healthCheckResult = mockMvc
        .perform(requestBuilder).andReturn();
    Assert.assertEquals(HttpStatus.OK.value(), healthCheckResult.getResponse().getStatus());
  }



}

The problem I have is my CustomLogger. Since it has external dependencies am having issues in trying to test this.The same kind of logger is present in my service classes too. How can I test such a class. I tried the below stuffs

  • Created a custom class name CustomLoggerForTest under test. Used ReflectionTestUtils.setField(healthService, "logger", new CustomerLoggerForTest(HealthService.class.getName())); in the setUp. But it did not help. Using this we cannot set the static fields hence tried even converting them to be non-static
  • Tried with mocking the CustomLogger in setup as below mockStatic(CustomLogger.class); when(CustomLogger.getLogger(any())) .thenReturn(new CustomLoggerForTest(HealthController.class.getName())); But no luck.
    Is there anything that am doing wrong that is causing this?
balaaagi
  • 502
  • 11
  • 21
  • 1
    Injejct the logger into the bean instead of creating it statically, mock it like every other dependency. See: [How do I inject a logger into a field in the sample spring boot application?](https://stackoverflow.com/questions/26517309/how-do-i-inject-a-logger-into-a-field-in-the-sample-spring-boot-application) (specifically [this answer](https://stackoverflow.com/a/55338237/4216641)) – Turing85 Sep 15 '20 at 19:19
  • Thanks it worked as expected if I do this way – balaaagi Sep 15 '20 at 20:19

0 Answers0