This recipe comes to amplify the previous two recipes with more details regarding JSFUnit API. You will see a set of snapshots that cover the main questions about writing JSFUnit test classes.
You can integrate the following code snippets into your JSFUnit test classes just by simple copy-pasting and then replacing the […] sections accordingly to your needs:
WebClientSpec webClientSpec = new WebClientSpec("/[PAGE_NAME]"); HtmlPage htmlPage = (HtmlPage)webClientSpec.doInitialRequest();
FacesContext
instance:FacesContext facesContext = FacesContextBridge.getCurrentInstance();
UIViewRoot uiViewRoot = facesContext.getViewRoot();
viewID
:assertEquals("/[PAGE_NAME]", uiViewRoot.getViewId());
UIComponent ui = uiViewRoot.findComponent("[FORM_ID]:[COMPONENT_ID]"); assertTrue(ui.isRendered());
UIComponent ui = uiViewRoot.findComponent("[FORM_ID]:[COMPONENT_ID]"); assertFalse(ui.isRendered());
inputText
component (it could be valid or invalid data):HtmlInput anInputText = (HtmlInput)htmlPage.getElementById("[FORM_ID]:[INPUT_TEXT_ID]"); anInputText.setValueAttribute("[VALUE_TO_POPULATE_INPUT_TEXT]"); HtmlSubmitInput htmlSubmitInput = (HtmlSubmitInput)htmlPage. getElementById("[FORM_ID]:[SUBMIT_BUTTON_ID]"); htmlSubmitInput.click();
FacesMessage
generated for the previous test (also you may want to check if the control returned to the initial state because of an error):FacesMessage message = (FacesMessage)facesContext.getMessages().next(); assertTrue(message.getDetail().contains("[INPUT_TEXT_ID]"));
UIComponent ui = uiViewRoot.findComponent("[FORM_ID]:[COMPONENT_ID]"); assertTrue(ui.isRendered()); assertEquals("[TEXT_TO_COMPARE]", ((ValueHolder)ui).getValue());
assertEquals("[TEXT_TO_COMPARE]", (String)facesContext.getApplication(). createValueBinding("#{[BEAN_PROPERTY]}").getValue(facesContext));
HtmlSubmitInput htmlSubmitInput = (HtmlSubmitInput)htmlPage. getElementById("[FORM_ID]:[BUTTON_ID]"); htmlSubmitInput.click();
JSFSession jsfSession = new JSFSession("/[PAGE_NAME]"); JSFClientSession client = jsfSession.getJSFClientSession(); JSFServerSession server = jsfSession.getJSFServerSession();
WebClientSpec webClientSpec = new WebClientSpec("/[PAGE_NAME]", BrowserVersion.INTERNET_EXPLORER_6_0); JSFSession jsfSession = new JSFSession(webClientSpec); assertEquals(BrowserVersion.INTERNET_EXPLORER_6_0, jsfSession.getWebClient().getBrowserVersion());
inputText
through JSF client session:client.setValue("[INPUT_TEXT_ID]", "[TEXT_TO_POPULATE]"); client.click("[SUBMIT_BUTTON_ID]");
client.click("[CHECKBOX_ID]"); // check/uncheck client.click("[SUBMIT_BUTTON_ID]"); assertFalse((Boolean)server.getManagedBeanValue("#{[BEAN_PROPERTY]}")); client.click("[CHECKBOX_ID]"); // check/uncheck client.click("[SUBMIT_BUTTON_ID]"); assertTrue((Boolean)server.getManagedBeanValue("#{[BEAN_PROPERTY]}"));
client.click("[NAVIGATION_BUTTON_ID]"); client.click("[COMMANDLINK_ID]"); // still on the same page ? assertEquals("/[PAGE_NAME]", server.getCurrentViewID());
assertEquals("[INITIAL_VALUE]", server.getManagedBeanValue("#{[BEAN_PROPERTY]}")); client.setValue("[TEXT_AREA_ID]", "[FINAL_VALUE]"); client.click("[SUBMIT_BUTTON_ID]"); assertEquals("[FINAL_VALUE]", server.getManagedBeanValue("#{[BEAN_PROPERTY]}"));
assertEquals("[SELECTED_CURRENT_RADIO_ITEM_VALUE]", server.getManagedBeanValue("#{[BEAN_PROPERTY]}")); client.click("[ANOTHER_RADIO_ID]"); client.click("[SUBMIT_BUTTON_ID]"); assertEquals("[SELECTED_ANOTHER_RADIO_ITEM_VALUE]", server.getManagedBeanValue("#{[BEAN_PROPERTY]}"));
selectManyListbox
:client.click("[SELECT_ITEM_ID_1]"); client.click("[SELECT_ITEM_ID_3]"); client.click("[SELECT_ITEM_ID_6]"); HtmlSelectManyListbox htmlSelectManyListbox = (HtmlSelectManyListbox)server. findComponent("[SELECT_MANY_LISTBOX_ID]"); Object[] selectedValues = htmlSelectManyListbox.getSelectedValues(); assertEquals(3, selectedValues.length); List listValues = Arrays.asList(selectedValues); assertTrue(listValues.contains("[SELECT_ITEM_VALUE_1]")); assertTrue(listValues.contains("[SELECT_ITEM_VALUE_3]")); assertTrue(listValues.contains("[SELECT_ITEM_VALUE_6]")); assertFalse(listValues.contains("[SELECT_ITEM_VALUE_2]")); assertFalse(listValues.contains("[SELECT_ITEM_VALUE_4]")); assertFalse(listValues.contains("[SELECT_ITEM_VALUE_5]"));
JSFTimer jsfTimer = JSFTimer.getTimer(); assertTrue(jsfTimer.getTotalTime() > 0);
client.setValue("[INPUT_TEXT_ID]", "[TEXT_TO_POPULATE]"); client.click("[SUBMIT_BUTTON_ID]"); JSFTimer jsfTimer = JSFTimer.getTimer(); assertTrue(jsfTimer.getTotalTime() >= 1000);
Well, this is just the beginning in exploring the JSFUnit API, but I think that you get the main idea of how JSFUnit works in the "testing world". If you are a JSF developer and you are familiar with JUnit, then it will be a piece of cake to understand and write JSFUnit tests.