HealthCheck { public Result execute() { long start = clock().getTick(); Result result; try { result = check(); } catch (Exception e) { result = Result.unhealthy(e); } result.setDuration(TimeUnit.MILLISECONDS.convert(clock().getTick() - start, TimeUnit.NANOSECONDS)); return result; }  Result execute();  }
@Test public void returnsResultsWhenExecuted() { final HealthCheck.Result result = mock(HealthCheck.Result.class); when(underlying.execute()).thenReturn(result); assertThat(healthCheck.execute()) .isEqualTo(result); verify(result).setDuration(anyLong()); }
@Test public void wrapsExceptionsWhenExecuted() { final RuntimeException e = mock(RuntimeException.class); when(e.getMessage()).thenReturn("oh noes"); when(underlying.execute()).thenThrow(e); HealthCheck.Result actual = healthCheck.execute(); assertThat(actual.isHealthy()) .isFalse(); assertThat(actual.getMessage()) .isEqualTo("oh noes"); assertThat(actual.getError()) .isEqualTo(e); assertThat(actual.getDetails()) .isNull(); assertThat(actual.getDuration()) .isGreaterThanOrEqualTo(0); }