我在eclipse中使用Spring MVC并在本地开发服务器上运行junit测试。 Junit测试无法访问GAE的数据存储区。在本地测试Junit时是否需要任何连接或设置来访问GAE dev服务器。 //代码如下
private LocalServiceTestHelper helper;
@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();
}
答案 0 :(得分:4)
如果您需要测试数据存储区(GAE),您应该按照步骤(here)添加jar
然后你可以测试一下:
public class DatastoreTest {
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void queryTest(){
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Entity directoty = new Entity("Directory");
directoty.setProperty("email", "test@localhost");
directoty.setProperty("name", "edy");
ds.put(directoty);
assertEquals(1,ds.prepare(new Query("Directoty")).countEntities(withLimit(10)));
}
}