java - 无法通过长 ID 删除实体

标签 java google-app-engine junit google-cloud-datastore

我有这个 JUnit 代码:

@Test
public void test(){
  Entity e = new Entity(KeyFactory.createKey("Post", 1L))
  Key key = _ds.put(e); // _ds is native DatastoreService
  _ds.delete(KeyFactory.createKey("Post", 1L));
  Entity deleted = _ds.get(KeyFactory.createKey("Post", 1L));
  assertNull(deleted); // Not null, why?
}

Entity无法删除可能是什么问题?

我尝试使用Key(由 put 生成)作为 key :

_ds.delete(key); // won't delete also? 

(虽然我真正需要找出为什么它不会通过长ID删除)

最佳答案

我稍微修改了你的测试(以编译),它对我有用:

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class StackoverflowTest {

    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

    @Before
    public void setUp() {
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }

    private final DatastoreService _ds = DatastoreServiceFactory.getDatastoreService();

    /**
     * http://stackoverflow.com/questions/28031142/cannot-delete-entity-by-long-id
     */
    @Test(expected = EntityNotFoundException.class)
    public void test28031142() throws EntityNotFoundException {
        Entity e = new Entity(KeyFactory.createKey("Post", 1L));
        Key key = _ds.put(e); // _ds is native DatastoreService
        _ds.delete(KeyFactory.createKey("Post", 1L));
        _ds.get(KeyFactory.createKey("Post", 1L));
    }
}

关于java - 无法通过长 ID 删除实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28031142/

相关文章:

java - 当线程崩溃时到底会发生什么?

java - 谷歌应用程序引擎上的NoSuchMethodError

python - Google App Engine 中的数据存储

java - 测试两个对象实例是否相等 JUnit

java - JUnit 和检查字段是否为空

java - 非阻塞 IO 与异步 IO 以及 Java 中的实现

java - 无法使用 javaFx Webview 在网页中显示视频(视频在给定的 url 上实时显示)

java - Java中静态字段的确切含义是什么?

python - 一周内在不同的间隔运行 cron

java - 测试 junit 时忽略扫描仪输入