JUnit testing with GWT and GAE

0 views
Skip to first unread message

Peerke via StackOverflow

unread,
Mar 22, 2013, 10:55:19 AM3/22/13
to google-appengin...@googlegroups.com

I want to have some JUnit test (created with GWT) to contact a servlet (with GAE calls inside). When I run the test I get the following stacktrace:

java.lang.NullPointerException
    at com.google.appengine.api.NamespaceManager.get(NamespaceManager.java:101)
    at com.google.appengine.api.memcache.BaseMemcacheServiceImpl.getEffectiveNamespace(BaseMemcacheServiceImpl.java:65)
    at com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.doGet(AsyncMemcacheServiceImpl.java:295)
    at com.google.appengine.api.memcache.AsyncMemcacheServiceImpl.contains(AsyncMemcacheServiceImpl.java:283)
    at com.google.appengine.api.memcache.MemcacheServiceImpl.contains(MemcacheServiceImpl.java:44)
    at com.peerkesoftware.etheria.web.server.model.UpdateTimer.<init>(UpdateTimer.java:17)
    at com.peerkesoftware.etheria.web.server.UpdateTimerServlet.<init>(UpdateTimerServlet.java:12)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
    at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

Can someone explain to me why this is happening? And how can I resolve this? Do I need to initialize something before running the test?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/15573365/junit-testing-with-gwt-and-gae

SSR via StackOverflow

unread,
Mar 24, 2013, 11:41:00 PM3/24/13
to google-appengin...@googlegroups.com

The line 101 throwing null pointer exception corresponds to https://code.google.com/p/googleappengine/source/browse/trunk/java/src/main/com/google/appengine/api/NamespaceManager.java

Map<String, Object> attributes =ApiProxy.getCurrentEnvironment().getAttributes();

I am guessing your ApiProxy.getCurrentEnvironment() is returning null.

Solution: Edit war/WEB-INF/web.xml to ApplicationServlet instead of GAEApplicationServlet.

I would like to point out that you unit tests for GWT client side should not be running so deep into server side. You should be using mocking or avoiding server side classes from being invoked from GWT tests to keep the GWT tests faster and client specific.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/15573365/junit-testing-with-gwt-and-gae/15607233#15607233

Peerke via StackOverflow

unread,
Mar 25, 2013, 11:06:29 AM3/25/13
to google-appengin...@googlegroups.com

I figured it out myself.

The solution that I used: Create LocalServiceTestHelper and set this up before doing any calls to my servlets. Then all my JUnit tests are green.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/15573365/junit-testing-with-gwt-and-gae/15618140#15618140

Peerke via StackOverflow

unread,
Mar 26, 2013, 5:13:47 AM3/26/13
to google-appengin...@googlegroups.com

I'm using GWT version 2.5.1 and GAE version 1.7.5.

My code can be viewed at GitHub.

step76 via StackOverflow

unread,
Mar 27, 2013, 7:46:57 AM3/27/13
to google-appengin...@googlegroups.com

The documentation to create JUnit tests for Google App Engine applications under java is here

https://developers.google.com/appengine/docs/java/tools/localunittesting



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/15573365/junit-testing-with-gwt-and-gae/15657996#15657996
Reply all
Reply to author
Forward
0 new messages