### Question:
ExternalServiceHealthCheck extends HealthCheck { @Override public Result check() throws Exception { LOGGER.debug("Check if Endpoint is available (URL: {})", this.url); int statusCode; try { HttpURLConnection connection = openConnection.call(url); try (AutoCloseable ignored = connection::disconnect) { connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setRequestMethod(requestMethod); statusCode = connection.getResponseCode(); if (HttpStatus.isSuccess(statusCode)) { LOGGER.debug("Endpoint is available (URL: {})", this.url); return Result.healthy(); } } } catch (IOException exception) { LOGGER.warn( "IOException (Details in Logs) - Endpoint not available (URL: {})", this.url, exception); return Result.unhealthy( "IOException (Details in Logs) - Endpoint not available (URL: %s)", this.url); } LOGGER.warn("Endpoint not available (URL: {})", this.url); return Result.unhealthy( "Endpoint not available (URL: %s, StatusCode: %s)", this.url, statusCode); } ExternalServiceHealthCheck(String url, int timeoutMillis);  ExternalServiceHealthCheck(String requestMethod, String url, int timeoutMillis); @SuppressWarnings("WeakerAccess")  ExternalServiceHealthCheck(
      String url, int timeoutMillis, OpenConnectionFunction openConnection);  ExternalServiceHealthCheck(
      String requestMethod, String url, int timeoutMillis, OpenConnectionFunction openConnection); @Override Result check();  }### Answer:
@Test public void testGetExternalServiceURLOk() throws Exception { Mockito.when(getConnectionMock.getResponseCode()).thenReturn(SC_OK); Result result = getHealthCheck.check(); assertTrue(result.isHealthy()); }
@Test public void testGetExternalServiceURLNoContent() throws Exception { Mockito.when(getConnectionMock.getResponseCode()).thenReturn(SC_NO_CONTENT); Result result = getHealthCheck.check(); assertTrue(result.isHealthy()); }
@Test public void testGetExternalServiceURLClientError() throws Exception { Mockito.when(getConnectionMock.getResponseCode()).thenReturn(SC_NOT_FOUND); Result result = getHealthCheck.check(); assertFalse(result.isHealthy()); }
@Test public void testGetExternalServiceURLServerError() throws Exception { Mockito.when(getConnectionMock.getResponseCode()).thenReturn(SC_INTERNAL_SERVER_ERROR); Result result = getHealthCheck.check(); assertFalse(result.isHealthy()); }
@Test public void testGetExternalServiceURLException() throws Exception { Mockito.when(getConnectionMock.getResponseCode()).thenThrow(new IOException()); Result result = getHealthCheck.check(); assertFalse(result.isHealthy()); }
@Test public void testHeadExternalServiceURLOk() throws Exception { Mockito.when(headConnectionMock.getResponseCode()).thenReturn(SC_OK); Result result = headHealthCheck.check(); assertTrue(result.isHealthy()); }
@Test public void testHeadExternalServiceURLNoContent() throws Exception { Mockito.when(headConnectionMock.getResponseCode()).thenReturn(SC_NO_CONTENT); Result result = headHealthCheck.check(); assertTrue(result.isHealthy()); }
@Test public void testHeadExternalServiceURLClientError() throws Exception { Mockito.when(headConnectionMock.getResponseCode()).thenReturn(SC_NOT_FOUND); Result result = headHealthCheck.check(); assertFalse(result.isHealthy()); assertThat(result.getMessage()).contains("404"); assertThat(result.getMessage()).contains("http: }
@Test public void testHeadExternalServiceURLServerError() throws Exception { Mockito.when(headConnectionMock.getResponseCode()).thenReturn(SC_INTERNAL_SERVER_ERROR); Result result = headHealthCheck.check(); assertFalse(result.isHealthy()); assertThat(result.getMessage()).contains("500"); assertThat(result.getMessage()).contains("http: }
@Test public void testHeadExternalServiceURLException() throws Exception { Mockito.when(headConnectionMock.getResponseCode()).thenThrow(new IOException()); Result result = headHealthCheck.check(); assertFalse(result.isHealthy()); assertThat(result.getMessage()).contains("http: }