You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Miguel Almeida <mi...@almeida.at> on 2012/01/27 18:35:42 UTC

Testing with StrutsTestCase in a Tiles application - NullPointerException

Hi there,

I trying to test an application with the help of StrutsTestCase (a class
that extends from StrutsSpringTestCase, to be more precise).
StrutsTestCase (which is in the struts junit plugin) involves proxied
actions to make tests more light.

I have used the same method in another application that doesn't have
tiles successfully. Here, however, I am running into an exception when
running the actionProxy.execute() :


java.lang.NullPointerException
	at
org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
	at
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)


I found a user that described the problem very well on stackOverflow:
http://stackoverflow.com/questions/5823709/npe-in-strutstestcase-after-enabling-tiles

There is also a 2008 question on the mailing list
(http://www.mail-archive.com/user@struts.apache.org/msg77582.html ).
Petrelli tried to answer, but no solution was posted.

>From these resources, do you know what the problem might be and if it is
possible to use StrutsTestCase to test an application that has Tiles
enabled through the StrutsTilesListener?


Cheers,

Miguel Almeida


Re: Testing with StrutsTestCase in a Tiles application - NullPointerException

Posted by Miguel Almeida <mi...@almeida.at>.
For future reference, I found the answer in
http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/

Basically, you need to initialize the tiles listener. As I already used
setupBeforeInitDispatcher to set the applicationContext attribute, I
added the code there (inside the if(tilesApplication) segment):

	/** 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);
        }

    }

Miguel Almeida


On Fri, 2012-01-27 at 17:35 +0000, Miguel Almeida wrote:

> Hi there,
> 
> I trying to test an application with the help of StrutsTestCase (a class
> that extends from StrutsSpringTestCase, to be more precise).
> StrutsTestCase (which is in the struts junit plugin) involves proxied
> actions to make tests more light.
> 
> I have used the same method in another application that doesn't have
> tiles successfully. Here, however, I am running into an exception when
> running the actionProxy.execute() :
> 
> 
> java.lang.NullPointerException
> 	at
> org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
> 	at
> org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
> 
> 
> I found a user that described the problem very well on stackOverflow:
> http://stackoverflow.com/questions/5823709/npe-in-strutstestcase-after-enabling-tiles
> 
> There is also a 2008 question on the mailing list
> (http://www.mail-archive.com/user@struts.apache.org/msg77582.html ).
> Petrelli tried to answer, but no solution was posted.
> 
> From these resources, do you know what the problem might be and if it is
> possible to use StrutsTestCase to test an application that has Tiles
> enabled through the StrutsTilesListener?
> 
> 
> Cheers,
> 
> Miguel Almeida
>