title: NullPointerException calling tearDown() on Google App Engine LocalSErviceTestHelper
body: <p>I am writing tests (integration and unit) for a Google App Engine Java app that relies heavily on Google App Engine services such as Memcache and Datastore. In order to test my application locally using these services, I have this line in the parent class of all my test cases:</p>
  
  <pre><code>private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig(), new LocalMemcacheServiceTestConfig());
  </code></pre>
  
  <p>I begin every test with this function call (via TestNG's <code>@BeforeMethod</code> annotation):</p>
  
  <pre><code>helper.setUp();
  </code></pre>
  
  <p>And I end every test with this function call (via TestNG's <code>@AfterMethod</code> annotation):</p>
  
  <pre><code>helper.tearDown();
  </code></pre>
  
  <p>(Here's a reference to <a href="http://testng.org/doc/documentation-main.html#annotations" rel="nofollow">TestNG annotations</a> and <a href="https://developers.google.com/appengine/docs/java/tools/localunittesting" rel="nofollow">local unit testing for Google App Engine's Java Runtime</a>, in case you need it. Especially for the latter link, note that my code strictly follows the examples given by Google)</p>
  
  <p>One thing to note is that one of my servlets in the Java app uses an instance of MemcacheService. It is injected into the servlet's constructor by Guice.</p>
  
  <p>Now, I build my code and run it via a call to <code>mvn clean install</code>, which will start an instance of <em>Jetty</em> and run my tests after the code has been compiled. <strong>Much to my chagrin,</strong> I get this stack trace printed before TestNG reports mass test failure:</p>
  
  <pre><code>SEVERE: Received exception tearing down config of type com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig
  java.lang.NullPointerException
  at com.google.appengine.tools.development.testing.LocalServiceTestHelper.getLocalService(LocalServiceTestHelper.java:495)
  at com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig.getLocalMemcacheService(LocalMemcacheServiceTestConfig.java:71)
  at com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig.tearDown(LocalMemcacheServiceTestConfig.java:47)
  at com.google.appengine.tools.development.testing.LocalServiceTestHelper.tearDown(LocalServiceTestHelper.java:438)
  at com.ea.pogosocial.AbstractTest.tearDown(AbstractTest.java:65)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:616)
  at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
  at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
  at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
  at org.testng.internal.Invoker.invokeMethod(Invoker.java:796)
  at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:907)
  at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1237)
  at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
  at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
  at java.lang.Thread.run(Thread.java:679)
  </code></pre>
  
  <p>I have tried almost everything I can think of to fix the problem, but I still run into this tearDown() problem, which is really stumping me. Beside put() and get(), I don't do anything fancy with the memcache instance in my servlet. Does anyone have any ideas as to what I should do?</p>
  
  <p>If more information or code is needed, I will be glad to provide. One thing to note: when I instead start my Jetty server with <code>mvn gae:run</code> and I execute the TestNG tests through Eclipse, I do not run into this issue. Perhaps this is because my servlet is being injected a non-local unit test helper instance of memcache, but rather an actual version of a memcache service.</p>