### Question:
HandlerUtil implements ApplicationListener<ContextRefreshedEvent> { public static <H, T> H getPreferredHandler(Class<H> handlerType, Class<T> type) { if (handlerType == null || type == null) { throw new IllegalArgumentException("You must specify both a handlerType and a type"); } List<H> handlers = getHandlersForType(handlerType, type); if (handlers == null || handlers.isEmpty()) { throw new APIException("handler.type.not.found", new Object[] { handlerType, type }); } if (handlers.size() > 1) { int order1 = getOrderOfHandler(handlers.get(0).getClass()); int order2 = getOrderOfHandler(handlers.get(1).getClass()); if (order1 == order2) { throw new APIException("handler.type.multiple", new Object[] { handlerType, type }); } } return handlers.get(0); }  static void clearCachedHandlers(); static List<H> getHandlersForType(Class<H> handlerType, Class<T> type); static H getPreferredHandler(Class<H> handlerType, Class<T> type); static Integer getOrderOfHandler(Class<?> handlerClass); @Override void onApplicationEvent(ContextRefreshedEvent event);  }### Answer:
@Test public void getPreferredHandler_shouldReturnThePreferredHandlerForThePassedHandlerAndType() { Validator v = HandlerUtil.getPreferredHandler(Validator.class, DrugOrder.class); Assert.assertEquals(DrugOrderValidator.class, v.getClass()); }
@Test public void getPreferredHandler_shouldThrowAAPIExceptionExceptionIfNoHandlerIsFound() { thrown.expect(APIException.class); thrown.expectMessage(Context.getMessageSourceService().getMessage("handler.type.not.found", new Object[] { Validator.class.toString(), Integer.class }, null)); HandlerUtil.getPreferredHandler(Validator.class, Integer.class); }
@Test public void getPreferredHandler_shouldReturnPatientValidatorForPatient() { Validator handler = HandlerUtil.getPreferredHandler(Validator.class, Patient.class); assertThat(handler, is(instanceOf(PatientValidator.class))); }
@Test public void getPreferredHandler_shouldReturnPersonValidatorForPerson() { Validator handler = HandlerUtil.getPreferredHandler(Validator.class, Person.class); assertThat(handler, is(instanceOf(PersonValidator.class))); }
@Test @Verifies(value = "should throw a APIException if no handler is found", method = "getPreferredHandler(Class, Class)") public void getPreferredHandler_shouldThrowAAPIExceptionExceptionIfNoHandlerIsFound() throws Exception { thrown.expect(APIException.class); thrown.expectMessage(Context.getMessageSourceService().getMessage("handler.type.not.found", new Object[] { Validator.class.toString(), Integer.class }, null)); HandlerUtil.getPreferredHandler(Validator.class, Integer.class); }
@Test @Verifies(value = "should return patient validator for patient", method = "getPreferredHandler(Class, Class)") public void getPreferredHandler_shouldReturnPatientValidatorForPatient() throws Exception { Validator handler = HandlerUtil.getPreferredHandler(Validator.class, Patient.class); assertThat(handler, is(instanceOf(PatientValidator.class))); }
@Test @Verifies(value = "should return person validator for person", method = "getPreferredHandler(Class, Class)") public void getPreferredHandler_shouldReturnPersonValidatorForPerson() throws Exception { Validator handler = HandlerUtil.getPreferredHandler(Validator.class, Person.class); assertThat(handler, is(instanceOf(PersonValidator.class))); }
@Test @Verifies(value = "should return the preferred handler for the passed handlerType and type", method = "getPreferredHandler(Class, Class)") public void getPreferredHandler_shouldReturnThePreferredHandlerForThePassedHandlerAndType() throws Exception { Validator v = HandlerUtil.getPreferredHandler(Validator.class, DrugOrder.class); Assert.assertEquals(DrugOrderValidator.class, v.getClass()); }