### Question:
HandlerUtil implements ApplicationListener<ContextRefreshedEvent> { public static <H, T> List<H> getHandlersForType(Class<H> handlerType, Class<T> type) { List<?> list = cachedHandlers.get(new Key(handlerType, type)); if (list != null) { return (List<H>) list; } List<H> handlers = new ArrayList<>(); log.debug("Getting handlers of type " + handlerType + (type == null ? "" : " for class " + type.getName())); for (H handler : Context.getRegisteredComponents(handlerType)) { Handler handlerAnnotation = handler.getClass().getAnnotation(Handler.class); if (handlerAnnotation != null) { if (type == null) { log.debug("Found handler " + handler.getClass()); handlers.add(handler); } else { for (int i = 0; i < handlerAnnotation.supports().length; i++) { Class<?> clazz = handlerAnnotation.supports()[i]; if (clazz.isAssignableFrom(type)) { log.debug("Found handler: " + handler.getClass()); handlers.add(handler); } } } } } handlers.sort(Comparator.comparing(o -> getOrderOfHandler(o.getClass()))); Map<Key, List<?>> newCachedHandlers = new WeakHashMap<>(cachedHandlers); newCachedHandlers.put(new Key(handlerType, type), handlers); cachedHandlers = newCachedHandlers; return handlers; }  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 getHandlersForType_shouldReturnAListOfAllClassesThatCanHandleThePassedType() { List<Validator> l = HandlerUtil.getHandlersForType(Validator.class, Order.class); Assert.assertEquals(1, l.size()); Assert.assertEquals(OrderValidator.class, l.iterator().next().getClass()); l = HandlerUtil.getHandlersForType(Validator.class, DrugOrder.class); Assert.assertEquals(2, l.size()); }
@Test public void getHandlersForType_shouldReturnAnEmptyListIfNoClassesCanHandleThePassedType() { List<Validator> l = HandlerUtil.getHandlersForType(Validator.class, PatientValidator.class); Assert.assertNotNull(l); Assert.assertEquals(0, l.size()); }