Test fails with java.lang.NullPointerException at org.jboss.
slabside Aug 13, 2008 11:14 AMHi
I have a problem with a NullPointer in my JSFUnit test. I am trying to test field input, assert the current view then assert some value within the FacesMessages.
Here's the test:
package uk.co.slc.cst.jsfunit; import java.io.IOException; import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import org.jboss.jsfunit.facade.JSFClientSession; import org.jboss.jsfunit.facade.JSFServerSession; import org.xml.sax.SAXException; import org.apache.cactus.ServletTestCase; import junit.framework.Test; import junit.framework.TestSuite; public class ViewBankDetailsIntegrationTest extends ServletTestCase { public static Test suite() { return new TestSuite(ViewBankDetailsIntegrationTest.class); } private JSFClientSession client; private static String viewId = "/ViewBankDetails.jsp"; private static String pageId = "/ViewBankDetails.faces"; private FacesMessage msg; private static String errorMsg = "Value is longer than allowable maximum of 8 characters"; public void setUp() throws IOException, SAXException { this.client = new JSFClientSession(pageId); } public void testViewBankDetailsPage() throws IOException, SAXException { // A JSFServerSession gives you access to JSF state JSFServerSession server = new JSFServerSession(client); // Test navigation to initial viewID assertEquals(viewId, server.getCurrentViewID()); // Assert that the components are in the component tree and rendered UIComponent component = server.findComponent("customerid"); assertTrue(component.isRendered()); component = server.findComponent("submitButton"); assertTrue(component.isRendered()); } public void testInvalidCustomerid() throws IOException, SAXException { this.client.setParameter("customerid", "111111111"); this.client.submit("submitButton"); JSFServerSession server = new JSFServerSession(client); // test view viewBankDetails displayed assertEquals(viewId, server.getCurrentViewID()); // test error message exists in message collection assertTrue(getMessage(server).getDetail().contains("customerid")); assertTrue(getMessage(server).getDetail().contains(errorMsg)); } /** * @return FacesMessage - the next message */ private FacesMessage getMessage(JSFServerSession server) { FacesMessage msg = (FacesMessage) server.getFacesContext().getMessages().next(); return msg; } }
Here is the jsp
<%@ page session="true" contentType="text/html;charset=utf-8"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%> <f:loadBundle basename="bundle.messages" var="msg"/> <f:loadBundle basename="bundle.errors" var="err"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>View Bank Details</title> </head> <body> <f:view> <h:form id="form1"> <h:outputLabel value="Please enter your "/> <h:outputLabel value="#{msg.customerId}"/> <!-- the input field with validation --> <h:inputText value="#{viewBankDetails.customerId}" id="customerid" required="true"> <f:validateLength minimum="1" maximum="8"/> <h:message for="customerid"/> </h:inputText> <h:commandButton value="Submit" id="submitButton" action="#{viewBankDetails.doSearch}"/> </h:form> </f:view> </body> </html>
I see other examples (by Stan) that use a ServerFacade..... should I be using that?
I also tried changing the way the field is referenced in the test from customerid to form1:customerid. this didn't work either.... the client is still null.
Sorry to be asking so many questions!
Any help much appreciated
Gerry