### Question:
HiveMetricsHelper { public static Optional<Long> incrementCounter(String name) { try { Metrics metrics = MetricsFactory.getInstance(); if (metrics != null) { return Optional.of(metrics.incrementCounter(name)); } } catch (Exception e) { log.warn("Unable to increment counter {}", name, e); } return Optional.empty(); } private  HiveMetricsHelper(); static Optional<Long> incrementCounter(String name);  }### Answer:
@Test public void nullMetricsFactory() { when(MetricsFactory.getInstance()).thenReturn(null); assertThat(HiveMetricsHelper.incrementCounter("name")).isNotPresent(); }
@Test public void exceptionInIncrementCounter() { when(MetricsFactory.getInstance()).thenReturn(metrics); when(metrics.incrementCounter("name")).thenThrow(RuntimeException.class); assertThat(HiveMetricsHelper.incrementCounter("name")).isNotPresent(); }
@Test public void incrementCounter() { when(MetricsFactory.getInstance()).thenReturn(metrics); when(metrics.incrementCounter("name")).thenReturn(123L); assertThat(HiveMetricsHelper.incrementCounter("name")).get().isEqualTo(123L); }