【发布时间】:2014-06-27 06:28:59
【问题描述】:
我在 Eclipse 中使用 Spring MVC 并在本地开发服务器中运行 junit 测试。 Junit 测试无法访问 GAE 的数据存储。在本地测试 Junit 时,访问 GAE 开发服务器是否需要任何连接或设置。 // 代码如下
私有 LocalServiceTestHelper 助手;
@Before
public void setUp()
{
File dbContents = new File("war/WEB-INF/appengine-generated/local_db.bin");
helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
.setBackingStoreLocation(dbContents.getAbsolutePath())
.setStoreDelayMs(4000)
.setNoStorage(true)
) {
};
this.helper.setUp();
LocalDatastoreService dsService = (LocalDatastoreService)LocalServiceTestHelper.getLocalService(LocalDatastoreService.PACKAGE);
dsService.setNoStorage(false);
CustomerPageJDO CustomerPageJDO = new CustomerPageJDO();
CustomerPageJDO.setCUID("fac54bca-d593-44c6-8c84-6b4d765eb35f");
accountJDO.setKey("fac54bca-d593-44c6-8c84-6b4d765eb35f");
accountJDO.setCompanyKey("fac54bca-d593-44c6-8c84-6b4d765eb35f");
accountJDO.setCurrentplanType("freePlan_v3");
planJDO.setPlanId("freePlan_v3");
planJDO.setPlanName("Free Plan");
pm = PMF.get().getPersistenceManager();
pm.makePersistent(CustomerPageJDO);
pm.makePersistent(accountJDO);
pm.makePersistent(planJDO);
}
public void testPersistaccountJDOs1()
{
List<AccountJDO> accountJDOList = null;
PersistenceManager pm;
//Ensure the accountJDO does not already exist in datasource
pm = PMF.get().getPersistenceManager();
boolean notFound = false;
try
{
accountJDO=pm.getObjectById(AccountJDO.class, "fac54bca-d593-44c6-8c84-6b4d765eb35f");
planJDO= pm.getObjectById(PlanJDO.class,accountJDO.getCurrentplanType());
System.out.println("accountJDO"+gson.toJson(accountJDO));
} catch (Exception e)
{
notFound = true;
System.out.println("error");
e.printStackTrace();
}
finally
{
pm.close();
}
}
@Test
public void testInsert2() {
testPersistaccountJDOs1();
}
@After
public void tearDown11()
{
this.helper.tearDown();
}
【问题讨论】:
-
对于初学者,您是否需要使用基于磁盘的数据存储进行测试?如果没有,使用仅内存方法会更简单。您遇到了什么错误或症状?
标签: java google-app-engine testing junit google-cloud-datastore