JSFUnit API

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.

How to do it...

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:

  • Start a JSFUnit session by getting a page:
    WebClientSpec webClientSpec = new WebClientSpec("/[PAGE_NAME]");
    HtmlPage htmlPage = (HtmlPage)webClientSpec.doInitialRequest();
    
  • Get the FacesContext instance:
    FacesContext facesContext =
    FacesContextBridge.getCurrentInstance();
    
  • Get the key to all state as of the last request:
    UIViewRoot uiViewRoot = facesContext.getViewRoot();
    
  • Test navigation to initial viewID:
    assertEquals("/[PAGE_NAME]", uiViewRoot.getViewId());
    
  • Assert that a component is in the component tree and rendered:
    UIComponent ui =
    uiViewRoot.findComponent("[FORM_ID]:[COMPONENT_ID]");
    assertTrue(ui.isRendered());
    
  • Assert that a component is in the component tree but it is not rendered:
    UIComponent ui =
    uiViewRoot.findComponent("[FORM_ID]:[COMPONENT_ID]");
    assertFalse(ui.isRendered());
    
  • Submit data for a 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();
    
  • Check the 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]"));
    
  • Assert that a component has the desired value:
    UIComponent ui = uiViewRoot.findComponent("[FORM_ID]:[COMPONENT_ID]");
    assertTrue(ui.isRendered());
    assertEquals("[TEXT_TO_COMPARE]", ((ValueHolder)ui).getValue());
    
  • Assert value for a backing bean property:
    assertEquals("[TEXT_TO_COMPARE]", (String)facesContext.getApplication().
    createValueBinding("#{[BEAN_PROPERTY]}").getValue(facesContext));
    
  • Simulate a button press:
    HtmlSubmitInput htmlSubmitInput = (HtmlSubmitInput)htmlPage.
    getElementById("[FORM_ID]:[BUTTON_ID]");
    htmlSubmitInput.click();
    
  • Start a JSFUnit session by getting a page:
    JSFSession jsfSession = new JSFSession("/[PAGE_NAME]");
    JSFClientSession client = jsfSession.getJSFClientSession();
    JSFServerSession server = jsfSession.getJSFServerSession();
    
  • Test browser version:
    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());
    
  • Populate and submit an inputText through JSF client session:
    client.setValue("[INPUT_TEXT_ID]", "[TEXT_TO_POPULATE]");
    client.click("[SUBMIT_BUTTON_ID]");
    
  • Check/uncheck a checkbox:
    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]}"));
    
  • Test a command link without view change:
    client.click("[NAVIGATION_BUTTON_ID]");
    client.click("[COMMANDLINK_ID]");
    // still on the same page ?
    assertEquals("/[PAGE_NAME]", server.getCurrentViewID());
    
  • Test a text area:
    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]}"));
    
  • Test a radio button:
    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]}"));
    
  • Test 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]"));
    
  • Test simple timing (example 1):
    JSFTimer jsfTimer = JSFTimer.getTimer();
    assertTrue(jsfTimer.getTotalTime() > 0);
    
  • Test simple timing (example 2).
    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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset