How to use AppStats with JUnit tests? (Google App Engine)

佐手、 提交于 2019-12-10 22:33:08

问题


I'm implementing a project for Google App Engine.

At this moment I have the following setup:

  • I have a couple of servlets in place to will give some data back to a client application (for example Android/iOS client)
  • To test this servlets I created junit tests accessing this servlets.
  • Inside the servlets there is some logic to determine if we are running tests or doing actually calls from a client. If we are running tests on a local machine we initialize a LocalServiceTestHelper to get the datastore, memcache, etc. working.

This all works fine.

Now comes the problem. When I add the AppStats filter to my web.xml, this also applies to the servlets when they are used in the locale junit tests. But the AppStats filter doesn't work. I get the following stacktrace when I run my tests:

java.lang.NullPointerException
    at com.google.appengine.tools.appstats.Recorder.checkNotNull(Recorder.java:378)
    at com.google.appengine.tools.appstats.Recorder.<init>(Recorder.java:210)
    at com.google.appengine.tools.appstats.Recorder.<init>(Recorder.java:200)
    at com.google.appengine.tools.appstats.AppstatsFilter.init(AppstatsFilter.java:183)
    at org.mortbay.jetty.servlet.FilterHolder.doStart(FilterHolder.java:97)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.servlet.ServletHandler.initialize(ServletHandler.java:593)
    at org.mortbay.jetty.servlet.Context.startContext(Context.java:140)
    at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1220)
    at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:513)
    at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:448)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.handler.RequestLogHandler.doStart(RequestLogHandler.java:115)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
    at org.mortbay.jetty.Server.doStart(Server.java:222)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:39)
    at com.google.gwt.dev.shell.jetty.JettyLauncher.start(JettyLauncher.java:672)
    at com.google.gwt.dev.DevMode.doStartUpServer(DevMode.java:509)
    at com.google.gwt.dev.DevModeBase.startUp(DevModeBase.java:1093)
    at com.google.gwt.junit.JUnitShell.getUnitTestShell(JUnitShell.java:707)
    at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:652)
    at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:441)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:296)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)

Anybody has any idea why the AppStats gets a NullPointerException?

As far as I see I have two options here:

  1. Get the AppStats filter to work in my current junit tests.
  2. Disable the AppStats filter for my locale junit tests but keep it active for normal calls to the servlets.

Both are fine, but I prefer to go with option 1.


Update april 19th 2013:

  • I tried initializing the MemCache before the filter (as mentioned below), but that doesn't seem to resolve anything. Same stacktrace as before.
  • From the Recorder.java source I figured out that probably the Delegate parameter in the constructor is null, but I have no idea why. (https://code.google.com/p/googleappengine/source/browse/trunk/java/src/main/com/google/appengine/tools/appstats/Recorder.java)

来源:https://stackoverflow.com/questions/16054339/how-to-use-appstats-with-junit-tests-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!