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