Menu

setActionForm returns NullPointerException

2004-02-07
2013-04-25
  • Nobody/Anonymous

    Hi,

    I am facing problem with setActionForm in MockStrutsTestCase.

    java.lang.NullPointerException
        at servletunit.struts.Common.setActionForm(Common.java:466)

    I debugged the program and the code that is returning this error was :
    if (actionConfig.getScope().equals("request"))  {

    in which actionConfig was null.

    The TestCase is as follows:

    File file = new File("I:\\jakarta-tomcat-5.0.14\\webapps\\FT") ;
    setContextDirectory(file);
    setConfigFile("employer","/WEB-INF/struts-config-employer.xml") ;
    setRequestPathInfo("employer","/EmprDetails.do");
    addRequestParameter("exec","Update");
    EmprForm updForm = new EmprForm() ;
    updForm.setComIDInteger(1) ;
    updForm.setComName("Automated Test Company");
    setActionForm(updForm);
    actionPerform();
    ...............

    the web.xml is configured as:
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config-default.xml</param-value>
        </init-param>
        <init-param>
          <param-name>config/employer</param-name>
          <param-value>/WEB-INF/struts-config-employer.xml</param-value>
        </init-param>

    the directory: I:\\jakarta-tomcat-5.0.14\\webapps\\FT is in the CLASSPATH

    I:\\jakarta-tomcat-5.0.14\\webapps\\FT\\WEB-INF has the following file:
    web.xml
    struts-config.xml
    struts-coinfig-employer.xml

    The action defined in the struts-config-employer.xml as below:

    <action
        path="/EmprDetails"
        type="examples.employer.action.EmprDetailsDispatchAction"
        name="EmployerForm"
        scope="request"
        validate="true"                parameter="exec">
        <forward    name="EmprDetailsView"    path="/pages/standard/employer/Emrdetailsview.jsp"/>
        <forward     name="EmprDetailsEdit"    path="/pages/standard/employer/Emrdetailsedit.jsp"/>           
    </action>

    Any clue on whats going wrong?

    TIA
    -Kumar

     
    • Nobody/Anonymous

      In extenstion to previous message:

      I:\\jakarta-tomcat-5.0.14\\webapps\\FT\\WEB-INF has the following file:
      web.xml
      struts-config.xml
      struts-config-default.xml
      struts-config-employer.xml

      Also, if I rename the file struts-config-employer.xml to struts-config.xml and do the necessary changes in web.xml and the setRequestPathInfo, the test is successful.

      This is the not the way my application has to be developed, so any help on why it does not work when set as a module?

      -Kumar

       
      • Bbp

        Bbp - 2008-05-05

        I got the same problem.

        I think StrutsTestCase need a default configuration. You can set it that way:

        ...
        setConfigFile("/WEB-INF/struts-config-employer.xml") ;
        setRequestPathInfo("/EmprDetails.do");
        ...

        If you have many profiles, you have to choose a default profile. Let say "struts-config-default.xml"
        Than you can do:
        ...
        setConfigFile("/WEB-INF/struts-config-default.xml") ;
        setConfigFile("employer","/WEB-INF/struts-config-employer.xml") ;
        setRequestPathInfo("/employer","/EmprDetails.do");
        ...

        That's work fine for me.

         
      • Bbp

        Bbp - 2008-05-05

        Oh, there is a bug when you manualy set a the form with "setActionForm" method.

        I found the solution in this thread:
        http://sourceforge.net/forum/message.php?msg_id=2610640

        The solution is to call a "deprecated" method from "RequestUtils" after called "setRequestPathInfo"

        import org.apache.struts.util.RequestUtils;
        ...
        public void testCreateWidget() {
            setConfigFile("myModule", "/WEB-INF/struts-config-myModule.xml");
            setRequestPathInfo("myModule", "/saveWidget");
            RequestUtils.selectModule(getRequest(), getActionServlet().getServletContext());
            WidgetForm myWidgetForm = new WidgetForm();
            [... initialize widget properties ...]
            setActionForm(myWidgetForm);
            actionPerform();
            verifyForward("success");
        }

        To avoid that error, i override the methods "setRequestPathInfo" in a wrapper:

        @Override
        public void setRequestPathInfo(final String path) {
            super.setRequestPathInfo(path);
            RequestUtils.selectModule(getRequest(), getActionServlet().getServletContext());
        }

        @Override
        public void setRequestPathInfo(final String module, final String path) {
            super.setRequestPathInfo(module, path);
            RequestUtils.selectModule(getRequest(), getActionServlet().getServletContext());
        }

        Deryl Seale said he will try to solve that bug.

         

Log in to post a comment.