You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Timothy Astle <ti...@caris.com> on 2009/03/05 14:33:12 UTC

Re: Writing a unit test that goes through result-types

I found an article on how to statically load Tiles to get past the 
container issue.  I found a web site that explained how to initialize 
the tiles container.  See below:

<snip>
    protected void setUp() throws Exception {
        if (applicationContext == null) {
            // this is the first time so initialize Spring context
            servletContext = new MockServletContext();
            servletContext.addInitParameter(
                    ContextLoader.CONFIG_LOCATION_PARAM, CONFIG_LOCATIONS);
            applicationContext = (new ContextLoader())
                    .initWebApplicationContext(servletContext);

            // Struts JSP support servlet (for Freemarker)
            new JspSupportServlet().init(new 
MockServletConfig(servletContext));

            // Below is the fix I needed.

            // Statically configuring tiles since we do not load it from the
            // web.xml for tests.
            servletContext.addInitParameter(
                    BasicTilesContainer.DEFINITIONS_CONFIG,
                    "/WEB-INF/tiles-def-test.xml");
            final StrutsTilesListener tilesListener = new 
StrutsTilesListener();
            final ServletContextEvent event = new ServletContextEvent(
                    servletContext);
            tilesListener.contextInitialized(event);
        }
.....
</snip>

Timothy Astle wrote:
> Thanks for the tip Greg.  I quickly went through the debugger based on 
> the example here: 
> http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/
>
> and I learned two things.
>
> 1.  execute() does call getSettings().  All that CRUD nonsense was me 
> not being sure it was actually calling the method I wanted because it 
> threw an exception upon execution.
>
> 2.  It looks like adding the following to the createAction(..) method 
> fixed my first problem.  I had an interceptor putting a variable on 
> the session, but no session was set, triggering a NullPointerException.
>
> proxy.getInvocation().getInvocationContext().setSession(new HashMap());
>
> Now I'm just running into a problem where the 
> TilesAccess.getContainer(servletContext) is returning a null.  This is 
> in the org.apache.struts2.views.tiles.TilesResult class.
>
>     public void doExecute(String location, ActionInvocation 
> invocation) throws Exception {
>         setLocation(location);
>
>         ServletContext servletContext = 
> ServletActionContext.getServletContext();
>         TilesContainer container = 
> TilesAccess.getContainer(servletContext);
>
>         HttpServletRequest request = ServletActionContext.getRequest();
>         HttpServletResponse response = ServletActionContext.getResponse();
>
> // fails because the container is null
>         container.render(location, request, response);
>     }
>
> I will post my final results when I get that part figured out.  If 
> someone can offer a tip, I'd appreciate it.  I'm learning as I go here.
>
> Tim
>
>
> Greg Lindholm wrote:
>> You can take a look at this:
>>
>> http://glindholm.wordpress.com/2008/06/30/unit-testing-struts-2-actions/
>>
>> I've used it to unit test actions all the way through interceptors,
>> validation, actions, and results including freemarker result types.
>>
>>
>> Timothy Astle wrote:
>>   
>>> I had tried using proxy.execute(), which will use Struts.  Maybe that's 
>>> where I'm a bit lost.  I don't implement execute() because of my CRUD 
>>> approach.
>>>
>>> Can someone point me to some appropriate struts 2 junit tests so I can 
>>> figure this out?
>>>
>>>
>>>     
>>
>>   
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org