### Question:
FacesMessagesUtils { @SuppressWarnings("unchecked") public int saveMessages(final FacesContext facesContext, final Map<String, Object> destination) { int savedCount = 0; if (facesContext != null) { Set<FacesMessageWrapper> messages = new LinkedHashSet<FacesMessageWrapper>(); for (Iterator<FacesMessage> iter = facesContext.getMessages(null); iter.hasNext();) { messages.add(new FacesMessageWrapper(iter.next())); } if (messages.size() > 0) { Set<FacesMessageWrapper> existingMessages = (LinkedHashSet<FacesMessageWrapper>) destination.get(token); if (existingMessages != null) { existingMessages.addAll(messages); } else { destination.put(token, messages); } savedCount = messages.size(); } } return savedCount; }  @SuppressWarnings("unchecked") int saveMessages(final FacesContext facesContext, final Map<String, Object> destination); @SuppressWarnings("unchecked") int restoreMessages(final FacesContext facesContext, final Map<String, Object> source);  }### Answer:
@Test public void testSaveMessages() { FacesContextMock facesContext = new FacesContextMock(); facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Summary A", "Detail A")); facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Summary B", "Detail B")); facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Summary B", "Detail B")); facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Summary B", "Detail B")); Map<String, Object> sessionMap = new HashMap<String, Object>(); int saved = new FacesMessagesUtils().saveMessages(facesContext, sessionMap); assertEquals(3, saved); assertNotNull(sessionMap.get(FacesMessagesUtils.token)); assertEquals(3, ((Collection<?>) sessionMap.get(FacesMessagesUtils.token)).size()); }