【问题标题】:Junit test not able to access the GAE datastore in local junit testJunit 测试无法访问本地 junit 测试中的 GAE 数据存储
【发布时间】: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


【解决方案1】:

如果您需要做测试数据存储 (GAE),您应该按照步骤 (here) 添加 jars

然后你可以测试例如:

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)));        
}

}

【讨论】:

    猜你喜欢
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多