簡體   English   中英

Objectify單元測試會將數據持久保存到Google Cloud Datastore

[英]Objectify Unit Tests are persisting data to Google Cloud Datastore

我有一個單元測試,嘗試按照此處找到的單元測試說明進行操作。 我發現,我保留的所有實體實際上都存儲在與我的Google Project相關聯的“真實” Google Cloud Platform數據存儲區中。

我的意圖是將實體保存到內存中,並在每次測試后拆除。

測試運行時,它似乎嘗試寫入本地數據存儲,但是所有內容最終都以某種方式寫入了雲數據存儲。 這是控制台輸出

com.google.appengine.api.datastore.dev.LocalDatastoreService初始化信息:本地數據存儲已初始化:類型:高復制存儲:內存中Jan 09,2019 10:44:49 AM com.google.appengine.api.datastore.dev .LocalDatastoreService初始化INFO:初始化本地數據存儲:類型:高復制存儲:內存中

這是我的單元測試

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的文檔。

新的數據存儲區API具有完全不同的模擬器。 您正在使用的本地測試工具就是與Objectify5一起使用的工具,或者曾經被稱為“ appengine數據存儲庫低級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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM