I have developed some JUnit tests which extend org.apache.struts2.StrutsTestCase. I used the tutorial on struts.apache.org as a starting point.
Everything worked fine until I changed my simple web application to use Tiles. I have Tiles that work perfectly in the application, but now my test cases have stopped working.
I get a NullPointerException in org.apache.struts2.views.tiles.TilesResult.doExecute when I run the following line of code:
ActionProxy proxy = getActionProxy("/displaytag.action");
The log shows that the Struts 2 action succeeds until it tries to pass it to TilesResult.doExecute.
I suspect this is because the tests run outside the container and the styles.xml file is only mentioned in the web.xml file, so my StrutsTestCase tests don't know where to find the definitions in the tiles.xml file.
Does that make sense?
I use Struts 2.2.1.1 and the plate-related jars (v. 2.0.6) included with the Struts distribution.
I will include a code snippet from my StrutsTestCase, but note that everything is successful when I launch the application from a browser in Tomcat, it just fails when I launch StrutsTestCase outside of Tomcat. And the test cases worked successfully before I added Tiles.
public class TagActionTest extends StrutsTestCase { static Logger logger = Logger.getLogger(TagActionTest.class); public void testCreateTagFail() throws Exception { logger.debug("Entering testCreateTagFail()"); try { request.setParameter("name", ""); ActionProxy proxy = getActionProxy("/createtag.action"); TagAction tagAction = (TagAction) proxy.getAction(); proxy.execute(); assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present", tagAction.getFieldErrors().size() == 1); assertTrue("Problem field 'name' not present in fieldErrors but it should have been", tagAction.getFieldErrors().containsKey("name") ); } catch (Exception e) { logger.debug("Error running testCreateTagFail()"); e.printStackTrace(); assertTrue("Error running testCreateTagFail()", false); } }
Partial Stack Trace:
java.lang.NullPointerException at org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105) at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186) at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
Finally, can anyone explain what a deal with StrutsTestCase is? There's a tutorial page to use with Struts 2 on struts.apache.org, but the SourceForge Page has not been updated for it since Struts 1.3 also, what's the difference between StrutsTestCase and MockStrutsTestCase