问题
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:
private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig(), new LocalMemcacheServiceTestConfig());
I begin every test with this function call (via TestNG's @BeforeMethod annotation):
helper.setUp();
And I end every test with this function call (via TestNG's @AfterMethod annotation):
helper.tearDown();
(Here's a reference to TestNG annotations and local unit testing for Google App Engine's Java Runtime, in case you need it. Especially for the latter link, note that my code strictly follows the examples given by Google)
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.
Now, I build my code and run it via a call to mvn clean install, which will start an instance of Jetty and run my tests after the code has been compiled. Much to my chagrin, I get this stack trace printed before TestNG reports mass test failure:
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)
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?
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 mvn gae:run 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.
回答1:
I had this exact same error. The problem for me was a mismatch in the version numbers for the Google App Engine SDK dependencies
I had:
- appengine-api-1.0-sdk: 1.8.1.1
- appengine-api-labs: 1.8.1.1
- appengine-api-stubs: 1.8.1.1
- appengine-testing: 1.8.1.1
- appengine-tools-sdk: 1.7.2
Once I updated appengine-tools-sdk to be the same version as the rest of the dependencies, my tests started passing again.
回答2:
I think you have slight issue how you have used @BeforeMethod and @AfterMethod annotations. Basically these annotations called before each test method executes and after each test method executes. you cannot assign @BeforeMethod and @AfterMethod annotations for individual test methods as you mentioned above.
And try try to implement your server startups in @BeforeClass or @BeforeSuite annotations. Therefor you can make sure the servers are up when the tests are executed.
回答3:
Just add those 3 libs (appart of the test one) to your classpath:
${SDK_ROOT}/lib/impl/appengine-api.jar
${SDK_ROOT}/lib/impl/appengine-api-labs.jar
${SDK_ROOT}/lib/impl/appengine-api-stubs.jar
This should fix the problem.
来源:https://stackoverflow.com/questions/12809336/nullpointerexception-calling-teardown-on-google-app-engine-localservicetesthel