### Question:
RangerValidator { RangerService getService(Long id) { if(LOG.isDebugEnabled()) { LOG.debug("==> RangerValidator.getService(" + id + ")"); } RangerService result = null; try { result = _store.getService(id); } catch (Exception e) { LOG.debug("Encountred exception while retrieving service from service store!", e); } if(LOG.isDebugEnabled()) { LOG.debug("<== RangerValidator.getService(" + id + "): " + result); } return result; } protected  RangerValidator(ServiceStore store); void validate(Long id, Action action);  }### Answer:
@Test public void test_getService_byId() throws Exception { when(_store.getService(1L)).thenReturn(null); when(_store.getService(2L)).thenThrow(new Exception()); RangerService service = mock(RangerService.class); when(_store.getService(3L)).thenReturn(service); assertNull(_validator.getService(1L)); assertNull(_validator.getService(2L)); assertTrue(_validator.getService(3L) != null); }
@Test public void test_getService() { try { when(_store.getServiceByName("return null")).thenReturn(null); when(_store.getServiceByName("throw")).thenThrow(new Exception()); RangerService service = mock(RangerService.class); when(_store.getServiceByName("good-service")).thenReturn(service); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception during mocking!"); } assertNull(_validator.getService("return null")); assertNull(_validator.getService("throw")); assertFalse(_validator.getService("good-service") == null); }