// class under test
FacesMessagesUtils utils = new FacesMessagesUtils();
// set up FacesContext
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 C", "Detail C"));
// call saveMessages()
Map<String, Object> sessionMap = new HashMap<String, Object>();
int saved = utils.saveMessages(facesContext, sessionMap);
// check message are saved
assertEquals(3, saved);
assertNotNull(sessionMap.get(FacesMessagesUtils.token));
assertEquals(3, ((Collection<?>) sessionMap.get(FacesMessagesUtils.token)).size());
/*
* Step 2: Restore
*/
// Setup new FacesContext with two messages
facesContext.clearAllMesages();
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Summary B", "Detail B"));
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Summary B", "Detail B"));
// restore messages
int restored = utils.restoreMessages(facesContext, sessionMap);
// check that two of the three messages have been restored