Objectify 单元测试正在将数据持久化到 Google Cloud Datastore

Objectify Unit Tests are persisting data to Google Cloud Datastore

我有一个单元测试试图遵循找到的单元测试说明 here。我发现我保留的任何实体实际上都存储在与我的 Google 项目关联的 "real" Google Cloud Platform DataStore 中。

我的意图是将实体持久保存到内存存储中,并在每次测试后拆除。

当测试运行时,它似乎试图写入本地数据存储,但不知何故一切最终都被写入云数据存储。这是控制台输出

com.google.appengine.api.datastore.dev.LocalDatastoreService init INFO: Local Datastore initialized: Type: High Replication Storage: In-memory Jan 09, 2019 10:44:49 AM com.google.appengine.api.datastore.dev.LocalDatastoreService init INFO: Local Datastore initialized: Type: High Replication Storage: In-memory

这是我的单元测试

public class AccountGCPDatastoreDaoTest {

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

private Closeable session;

private AccountGCPDatastoreDao dao = new AccountGCPDatastoreDao();

@BeforeClass
public static void setUpBeforeClass() {
    System.out.println("init Objectify");
    ObjectifyService.init(new ObjectifyFactory());
    ObjectifyService.register(Account.class);
}

@Before
public void setUp() { ;
    helper.setUp();
    session = ObjectifyService.begin();
}

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


@Test
public void save() {
    Account account = new Account(1L, "Test Account", false, AccountType.SMALL);
    dao.save(account);

    Account retAcc = ofy().load().type(Account.class).id(1).now();
    assertEquals(new Long(1),retAcc.getId());
    assertEquals("Test Account",retAcc.getName());
    assertFalse(retAcc.getPaidSubscription());
    assertEquals(AccountType.SMALL,retAcc.getAccountType());
}


@Test
public void findAccountsForRegistration_NoAccountsExist() {

    List<Account> accountsForRegistration = dao.findAccountsForRegistration();
    assertEquals(0,accountsForRegistration.size());


}

我的帐户实体:

@Entity
public class Account {

@Id private Long id;
private String name;
private boolean paidSubscription;
@Index private AccountType accountType;

private Account(){}

public Account(Long id,
               String name,
               boolean paidSubscription,
               AccountType accountType){
    this.id = id;
    this.name = name;
    this.paidSubscription = paidSubscription;
    this.accountType = accountType;
}

public Long getId() {
    return id;
}

public String getName() {
    return name;
}

public AccountType getAccountType(){
    return accountType;
}

public boolean getPaidSubscription() {
    return paidSubscription;
}

}

看起来您正在使用 Objectify6,它使用新的专用数据存储 API,但您正在查看旧的 appengine-everything-combined API.[=12 的文档=]

新的数据存储 API 有一个完全不同的模拟器。您正在使用的本地测试工具是您将与 Objectify5 一起使用的工具或过去称为 "appengine datastore low level API".

的工具

关于为新 API 设置测试环境的文档我上次看的时候不是很好。我建议查看 Objectify 的测试工具本身。如果您想从 JUnit4 升级,您实际上可以直接使用 Objectify JUnit5 拦截器。否则应该不难弄清楚发生了什么。

主要内容如下:

https://github.com/objectify/objectify/blob/master/src/test/java/com/googlecode/objectify/test/util/LocalDatastoreExtension.java https://github.com/objectify/objectify/blob/master/src/test/java/com/googlecode/objectify/test/util/ObjectifyExtension.java