I have a Unit Test that attempts to follow the Unit Test instructions found here. I find that any entities that I persist are actually stored in the "real" Google Cloud Platform DataStore associated with my Google Project.
My intention was that the Entities would be persisted to In Memory storage and torn down after each test.
When the test runs it appears that it tries to write to the Local Datastore, but everything somehow ends up being written to the Cloud Datastore. Here's the console output
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
Here's my Unit Test
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());
}
My Account entity :
@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;
}
}