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); protected  RangerValidator(RoleStore roleStore); void validate(Long id, Action action); void validate(String name, Action action); static String serializeFailures(List<ValidationFailureDetails> failures);  }
@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); Assert.assertNull(_validator.getService(1L)); Assert.assertNull(_validator.getService(2L)); Assert.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(); Assert.fail("Unexpected exception during mocking!"); } Assert.assertNull(_validator.getService("return null")); Assert.assertNull(_validator.getService("throw")); Assert.assertFalse(_validator.getService("good-service") == null); }