You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Dongmei Cao <do...@icann.org> on 2010/07/28 08:02:04 UTC

NPE from unit testing with equanda-tapestry5

Hi,

I use the equanda/Tabs in my web application. It actually works pretty well except it throws NPE in unit testing:

ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception: java.lang.NullPointerException
java.lang.NullPointerException
    at org.equanda.tapestry5.services.EquandaModule$1.service(EquandaModule.java:83)

Here is the code from EquandaModule.java that fails:

public static RequestFilter buildUtf8Filter(
        @InjectService( "RequestGlobals" ) final RequestGlobals requestGlobals )
    {
        return new RequestFilter()
        {
            public boolean service( Request request, Response response, RequestHandler handler )
                throws IOException
            {
                requestGlobals.getHTTPServletRequest().setCharacterEncoding( "UTF-8" );  // <---------------------- NPE on this line due to null HttpServletRequest
                return handler.service( request, response );
            }
        };
    }

Anyone has any idea how to get around this in unit testing?

Thanks in advance!

-D

Re: NPE from unit testing with equanda-tapestry5

Posted by Josh Canfield <jo...@gmail.com>.
I found this on the list some time ago. You can add the
HttpServletRequest/Response in a test module.

public class PageTesterModule {
    // Ensure that there are valid HTTP request/response objects in the test

    public static void contributeRequestHandler(
            final OrderedConfiguration<RequestFilter> config,
            final RequestGlobals requestGlobals) {

        RequestFilter filter = new RequestFilter() {
            public boolean service(Request request, Response response,
RequestHandler handler) throws IOException {
                requestGlobals.storeServletRequestResponse(
                        EasyMock.createMock(HttpServletRequest.class),
                        EasyMock.createMock(HttpServletResponse.class)
                );
                return handler.service(request, response);
            }
        };
        config.add("EnsureNonNullHttpRequestAndResponse", filter, "before:*");
    }
}


If you are using PageTester you can add the module this way:


 @BeforeClass
    public void setupPageTester() {
        String appPackage = "com.bodylab.gymserver";
        String appName = "app";
        _pageTester = new PageTester(appPackage, appName,
"src/main/webapp", PageTesterModule.class);
    }


Josh

On Tue, Jul 27, 2010 at 11:02 PM, Dongmei Cao <do...@icann.org> wrote:
> Hi,
>
> I use the equanda/Tabs in my web application. It actually works pretty well except it throws NPE in unit testing:
>
> ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception: java.lang.NullPointerException
> java.lang.NullPointerException
>    at org.equanda.tapestry5.services.EquandaModule$1.service(EquandaModule.java:83)
>
> Here is the code from EquandaModule.java that fails:
>
> public static RequestFilter buildUtf8Filter(
>        @InjectService( "RequestGlobals" ) final RequestGlobals requestGlobals )
>    {
>        return new RequestFilter()
>        {
>            public boolean service( Request request, Response response, RequestHandler handler )
>                throws IOException
>            {
>                requestGlobals.getHTTPServletRequest().setCharacterEncoding( "UTF-8" );  // <---------------------- NPE on this line due to null HttpServletRequest
>                return handler.service( request, response );
>            }
>        };
>    }
>
> Anyone has any idea how to get around this in unit testing?
>
> Thanks in advance!
>
> -D
>



-- 
--
http://www.bodylabgym.com - a private, by appointment only, one-on-one
health and fitness facility.
--
http://www.ectransition.com - Quality Electronic Cigarettes at a
reasonable price!
--
TheDailyTube.com. Sign up and get the best new videos on the internet
delivered fresh to your inbox.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org