Khalid Salman - 2014-02-11

Hello everyone,

I have a classic Netbeans web app project setup. When I try to run simple login action test, it returns null-pointer-exception.

Code:

public void testSuccessfulLogin() {
    setRequestPathInfo("/login.do");
    addRequestParameter("username","test-user");
    addRequestParameter("password","xxxyyyzzz");
    //assertNotNull(getActionServlet());
    actionPerform();
    verifyForward("success");
}

Output:

java.lang.NullPointerException
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:331)
at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:394)
at test.com.yesekicsek.input.TestLoginAction.testSuccessfulLogin(TestLoginAction.java:25)

And if I un-comment assertNotNull(getActionServlet()); line:

public void testSuccessfulLogin() {
    setRequestPathInfo("/login.do");
    addRequestParameter("username","test-user");
    addRequestParameter("password","xxxyyyzzz");
    assertNotNull(getActionServlet());
    actionPerform();
    verifyForward("success");
}

Then I get the following:

java.lang.NullPointerException
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:331)
at test.com.yesekicsek.input.TestLoginAction.testSuccessfulLogin(TestLoginAction.java:24)

In order to add the folder containing "WEB-INF" (which is "web" folder under the project root in a classic netbeans web app project setup) to the classpath, went to "Project Properties -> Libraries -> Run Tests -> Add JAR/Folder" and added "web" folder to classpath. But that didn't seem to be helping a lot.

Here is an overview of default netbeans web app project setup:

project-root/
|-nbproject/
|-src/
  |--conf/
  |--java/
    |--com.my-struts-actions.package
|-test/
  |--test/
    |--com.my-testing.package
|-web/
    |--WEB-INF/
        |--classes/
        |--struts-config.xml
        |--web.xml
    |--META-INF/
    |--various jsps

Any help would be really appreciated.
Thanks.