6 Replies Latest reply on Aug 20, 2008 9:41 AM by gerry N/A

    Test fails with java.lang.NullPointerException at org.jboss.

    gerry N/A Newbie

      Hi

      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



        • 1. Re: Test fails with java.lang.NullPointerException at org.jb
          Stan Silvert Master

          Can you post the stack trace for this? From your description, I can't tell where the failure is coming from.

          Stan

          • 2. Re: Test fails with java.lang.NullPointerException at org.jb
            gerry N/A Newbie

            Here's the stack trace Stan

            testViewBankDetailsPage Error null

            java.lang.NullPointerException at org.jboss.jsfunit.facade.DOMUtil.findElementWithAttribValue(DOMUtil.java:84) at org.jboss.jsfunit.facade.DOMUtil.findElementWithID(DOMUtil.java:69) at org.jboss.jsfunit.facade.JSFClientSession.setValueOnDOM(JSFClientSession.java:255) at org.jboss.jsfunit.facade.JSFClientSession.setParameter(JSFClientSession.java:274) at uk.co.slc.cst.jsfunit.ViewBankDetailsIntegrationTest.testViewBankDetailsPage(ViewBankDetailsIntegrationTest.java:48) at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153) at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:217) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java) at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101) at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:217) at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java) at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72) at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:217) at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java) at javax.servlet.http.HttpServlet.service(HttpServlet.java:743) at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65) at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:122) at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:623) at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370) at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871) at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453) at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:221) at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:122) at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:111) at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260) at oracle.oc4j.network.ServerSocketAcceptHandler.procClientSocket(ServerSocketAcceptHandler.java:239) at oracle.oc4j.network.ServerSocketAcceptHandler.access$700(ServerSocketAcceptHandler.java:34) at oracle.oc4j.network.ServerSocketAcceptHandler$AcceptHandlerHorse.run(ServerSocketAcceptHandler.java:880) at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303) at java.lang.Thread.run(Thread.java:595)

            thanks

            Gerry

            • 3. Re: Test fails with java.lang.NullPointerException at org.jb
              Stan Silvert Master

              The stack trace doesn't match your code. The code you posted doesn't have a setParameter() call in testViewBankDetailsPage().

              at org.jboss.jsfunit.facade.JSFClientSession.setParameter(JSFClientSession.java:274)
              at uk.co.slc.cst.jsfunit.ViewBankDetailsIntegrationTest.testViewBankDetailsPage(ViewBankDetailsIntegrationTest.java:48) at


              It's hard to tell where the NPE is really coming from. It looks like the private member variable, updatedDOM, is not set in JSFClientSession. But I can't tell for sure and it really doesn't make sense. If that were the case you would also see a TransformerException. Maybe you did see a TransformerException?

              Anyhow, I suggest that you try out the new HtmlUnit-based code that we will release in a couple of weeks. You can build it from SVN. Just look at the org.jboss.jsfunit.jsfsession package which will replace the org.jboss.jsfunit.facade package. Then start your tests like this:
              JSFSession jsfSession = new JSFSession("/ViewBankDetails.faces");
              JSFClientSession client = jsfSession.getJSFClientSession();
              JSFServerSession server = jsfSession.getJSFServerSession();


              The main difference in the API is the JSFClientSession. But it should be easy to pick up. I think it's simpler than the old one and you get the full power of HTMLUnit.

              Stan

              • 4. Re: Test fails with java.lang.NullPointerException at org.jb
                gerry N/A Newbie

                Doh... must have been in a hurry to leave on Friday...

                I don't see a TransformerException....

                This is the code I am executing:

                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 com.meterware.httpunit.GetMethodWebRequest;
                import com.meterware.httpunit.WebConversation;
                import com.meterware.httpunit.WebForm;
                import com.meterware.httpunit.WebRequest;
                import com.meterware.httpunit.WebResponse;
                
                import junit.framework.Test;
                import junit.framework.TestSuite;
                
                public class ViewBankDetailsIntegrationTest extends ServletTestCase {
                
                 public static Test suite() {
                 return new TestSuite(ViewBankDetailsIntegrationTest.class);
                 }
                
                 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 {
                
                 }
                
                 public void testViewBankDetailsPage() throws IOException, SAXException {
                
                 JSFClientSession client = new JSFClientSession("/ViewBankDetails.jsf");
                 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());
                 client.setParameter("customerid", "123456789");
                 client.submit();
                 assertEquals("123456789", server.getManagedBeanValue("#{viewBankDetails.customerId}"));
                
                 }
                
                }
                


                This is the error

                null

                java.lang.NullPointerException at org.jboss.jsfunit.facade.DOMUtil.findElementWithAttribValue(DOMUtil.java:84) at org.jboss.jsfunit.facade.DOMUtil.findElementWithID(DOMUtil.java:69) at org.jboss.jsfunit.facade.JSFClientSession.setValueOnDOM(JSFClientSession.java:255) at org.jboss.jsfunit.facade.JSFClientSession.setParameter(JSFClientSession.java:274) at uk.co.slc.cst.jsfunit.ViewBankDetailsIntegrationTest.testViewBankDetailsPage(ViewBankDetailsIntegrationTest.java:48) at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153) at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:217) at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java) at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101) at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:217) at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java) at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72) at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:217) at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java) at javax.servlet.http.HttpServlet.service(HttpServlet.java:743) at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65) at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:122) at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:623) at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:370) at com.evermind.server.http.HttpRequestHandler.doProcessRequest(HttpRequestHandler.java:871) at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:453) at com.evermind.server.http.HttpRequestHandler.serveOneRequest(HttpRequestHandler.java:221) at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:122) at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:111) at oracle.oc4j.network.ServerSocketReadHandler$SafeRunnable.run(ServerSocketReadHandler.java:260) at oracle.oc4j.network.ServerSocketAcceptHandler.procClientSocket(ServerSocketAcceptHandler.java:239) at oracle.oc4j.network.ServerSocketAcceptHandler.access$700(ServerSocketAcceptHandler.java:34) at oracle.oc4j.network.ServerSocketAcceptHandler$AcceptHandlerHorse.run(ServerSocketAcceptHandler.java:880) at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:303) at java.lang.Thread.run(Thread.java:595)


                From where I am sitting I don't have access to download and build from svn... can I download a nightly build in jar format?

                cheers

                Gerry







                • 5. Re: Test fails with java.lang.NullPointerException at org.jb
                  Stan Silvert Master

                  Hi Gerry,

                  Sorry you are stuck for the moment. I'm going to try to get the Beta 3 release out this weekend. So you should be able to download it soon.

                  Can you hold out until then?

                  Stan

                  • 6. Re: Test fails with java.lang.NullPointerException at org.jb
                    gerry N/A Newbie

                    Yes Stan - no problem.

                    I'll keep an eye out for beta3.

                    thanks!

                    Gerry