NPE in StrutsTestCase after turning on tile

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

+3
source share
2 answers

I assume that you initialize the tiles with the listener:

 <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> 

You need to initialize this listener in your tests. I found several others with the same question [1]. The code below is in your class that extends StrutsSpringTestCase. You need to override setupBeforeInitDispatcher. In the code snippet below, the redefinition sets the applicationContext attribute (also necessary if you use spring) and initializes Tiles (inside the if (tilesApplication) segment, where tilesApplication is logical, so you can switch this code on off based on whether your application is running using plates):

  /** Overrides the previous in order to skip applicationContext assignment: context is @autowired * @see org.apache.struts2.StrutsSpringTestCase#setupBeforeInitDispatcher() **/ @Override protected void setupBeforeInitDispatcher() throws Exception { //init context servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); if(tilesApplication){ servletContext.addInitParameter(BasicTilesContainer.DEFINITIONS_CONFIG, "WEB-INF/tiles.xml"); final StrutsTilesListener tilesListener = new StrutsTilesListener(); final ServletContextEvent event = new ServletContextEvent(servletContext); tilesListener.contextInitialized(event); } } 

[1] See http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/

+3
source

He is trying to display the jsp page. Therefore, disable by adding ExecuteResult (false) to the code.

So add the line below

 proxy.setExecuteResult(false); 

before proxy.execute()

+1
source

Source: https://habr.com/ru/post/891449/


All Articles