I have a unit test that calls the following code:
public PhoneNumber<Merchant> addContactNum(String number, PhoneNumber.Type type) {
type = (null == type) ? Type.WORK : type;
if (isNotBlank(number)) {
PhoneNumber<Merchant> phone = PhoneNumber.<Merchant> builder().number(number).type(type)
.build();
this.phoneRefs.add(phone);
phone.setPhoneNumberFor(this);
// ofy().defer().save().entity(phone);
ofy().save().entity(phone);
return phone;
}
return null;
}
If I use the deferred save version, it fails, otherwise it passes.
The failure happens in the tearDown() method of the JUnit (4) test class, when the Closeable returned by ObjectifyService.begin() is closed. The failure stack trace is shown below. Is this a known issue? Is the code from setUp() and tearDown() methods required? This is the first time I've used deferred saves.
java.lang.NullPointerException: No API environment is registered for this thread.
at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:180)
at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppIdNamespace(DatastoreApiHelper.java:196)
at com.google.appengine.api.datastore.Key.<init>(Key.java:96)
at com.google.appengine.api.datastore.Key.<init>(Key.java:86)
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:84)
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:77)
at com.googlecode.objectify.util.DatastoreUtils.createKey(DatastoreUtils.java:84)
at com.googlecode.objectify.impl.KeyMetadata.getRawKeyOrNull(KeyMetadata.java:192)
at com.googlecode.objectify.impl.KeyMetadata.getRawKey(KeyMetadata.java:202)
at com.googlecode.objectify.impl.Keys.rawKeyOf(Keys.java:36)
at com.googlecode.objectify.impl.Keys.keyOf(Keys.java:29)
at com.googlecode.objectify.impl.Deferrer.undefer(Deferrer.java:52)
at com.googlecode.objectify.impl.WriteEngine.save(WriteEngine.java:73)
at com.googlecode.objectify.impl.SaverImpl.entities(SaverImpl.java:60)
at com.googlecode.objectify.impl.Deferrer.flush(Deferrer.java:109)
at com.googlecode.objectify.impl.ObjectifyImpl.flush(ObjectifyImpl.java:307)
at com.googlecode.objectify.ObjectifyService$2.close(ObjectifyService.java:118)
at in.co.amebatechnologies.empireapp.test.DatastoreTests.tearDown(DatastoreTests.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Regards,
Harshdeep S Jawanda