StateRepository { public S read() { S state = null; try { if (repository.exists()) { state = repository.get(); } else { log.info("State does not exist for source {}", sourceName); } } catch (Exception ex) { log.error("Failed to read state for source " + sourceName, ex); metrics.stateReadFailure(ex); throw new RuntimeException(ex); } log.debug("Read state for source {}. state={}", sourceName, state); metrics.stateRead(); return state; }  void save(@NonNull final S state); S read();  }
@Test public void testRead() throws Exception { MysqlSourceState state = mock(MysqlSourceState.class); when(repository.get()).thenReturn(state); when(repository.exists()).thenReturn(false); assertNull(stateRepository.read()); when(repository.exists()).thenReturn(true); Assert.assertEquals(state, stateRepository.read()); verify(metrics, times(2)).stateRead(); }
@Test(expected = RuntimeException.class) public void testReadFailure() throws Exception { when(repository.exists()).thenThrow(new RuntimeException()); try { stateRepository.read(); } catch (RuntimeException ex) { verify(metrics, times(1)).stateReadFailure(any(Exception.class)); throw ex; } }