StrutsTestCase - Initial Configuration
Today I started learning about StrutsTestCase - it seemed like an exciting tool for building test cases for Struts-based applications. But, as is often the case in Javaland, the initial configuration proved to be quite challenging. Below is my experience and its resolution.
Here was my original stack trace, as seen in the Junit plugin:
servletunit.struts.ExceptionDuringTestError: A NullPointerException was thrown. This may indicate an error in your ActionForm, or it may indicate that the Struts ActionServlet was unable to find struts config file. TestCase is running from C:\workspace\HibernateTest directory. Context directory is C:\workspace\HibernateTest\WebContent. struts config file must be found under the context directory, the directory the test case is running from, or in the classpath.
servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:407)
....
------------
Root Cause:
------------
java.lang.NullPointerException
-servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:331)
Based on this, I started playing with all types of variants of setContextDirectory, setConfigFile, setServletConfigFile, etc. etc. But, what I had missed was that in the Eclipse console was the message:
16:50:20,968 ERROR [MessageResourcesFactory] MessageResourcesFactory.createFactory
java.lang.NoClassDefFoundError: javax/servlet/jsp/JspException
Seeing that, I added javax.servlet.jsp.jar to my build path in Eclipse.
Then, I hit a new problem that looked remarkably similar:
junit.framework.AssertionFailedError: Error initializing action servlet: Unable to find /WEB-INF/web.xml. TestCase is running from C:\workspace\HibernateTest directory. Context directory has not been set. Try calling setContextDirectory() with a relative or absolute path. /WEB-INF/web.xml must be found under the context directory, the directory the test case is running from, or in the classpath.
-junit.framework.Assert.fail(Assert.java:47)
-servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:342)
So, I kept experimenting with various combinations and incantations. Eventually I focused on the classpath, so I tried to add "WebContent" to the CP, as that is the directory that holds the WEB-INF. But try as I might, I could not get Eclipse to allow me to add it to the buildpath for the project. And then I realized that I could get it in the CP by adding it to the Run configuration that I was using to run the Junit test.
And then it worked! I think StrutsTestCase has tremendous potential, but it did take me 3 hours to get a completely empty test to run :).