1

我的设置:

win 7,eclipse,GAE sdk 1.8.5,objectify 4,junit4

我正在尝试编写 Junit 测试用例来保存两个 Car 实体并检索它们。我正在使用它,它工作得很好。

我的问题是我需要在多次运行的测试用例中保留这些实体,这意味着一旦我在一次运行中使用 doSaveCar() 保存,在下一次运行中,我应该在运行 doListCar() 时取回这两个实体. 如何在 eclipse环境中的各种 junit 测试运行中保持本地存储?

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class OFYTest  {

    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
            new LocalDatastoreServiceTestConfig(),
            new LocalMemcacheServiceTestConfig());

        @Before
        public void setUp() {
            helper.setUp();

        }
        @After
        public void tearDown() {
            helper.tearDown();
        }

        @Test
        public void doSaveCar() {

            new CRUDServiceImpl().save(new Car("Mustang", "green"));
            new CRUDServiceImpl().save(new Car("SRT", "red"));

        }


        @Test
        public void doListCars(){

            new CRUDServiceImpl().list(AbstractEntity.class);


        }

}
4

1 回答 1

1

NoStorage通过设置标志,我使用 objectify、junit、mockito 和 google-app-engine 将数据从一次运行保存到另一次运行。

dsService.setNoStorage(false);

参考下面的junit测试

private final LocalServiceTestHelper helper =
          new LocalServiceTestHelper(new LocalUserServiceTestConfig())
              .setEnvIsLoggedIn(true)
              .setEnvAuthDomain("localhost")
              .setEnvEmail("test@localhost");

  @Before
  public void setupCustomerServlet() {
    helper.setUp();
    LocalDatastoreService dsService = (LocalDatastoreService)helper.getLocalService(LocalDatastoreService.PACKAGE);
    dsService.setNoStorage(false);
    customerServlet = new CustomerServlet();
  }

  @After
  public void tearDownHelper() {
    helper.tearDown();
  }

  @Test
  public void testDoGet() throws IOException, ServletException {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);

    // A test entry
    Customer customerIn = new Customer();
    customerIn.setName("Felix");
    customerIn.setAddress("Kuestahler");
    String json = (new Gson()).toJson(customerIn);

    StringWriter stringWriter = new StringWriter();

    when(request.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
    when(response.getWriter()).thenReturn(new PrintWriter(stringWriter));

    customerServlet.doPost(request, response);

    Customer customerOut =  (new Gson()).fromJson(stringWriter.toString(), Customer.class);

    assertEquals(customerOut.getName(), customerIn.getName());
    assertEquals(customerOut.get_id() > 0,true);

    stringWriter = new StringWriter();

    when(request.getReader()).thenReturn(new BufferedReader(new StringReader("")));
    when(response.getWriter()).thenReturn(new PrintWriter(stringWriter));

    customerServlet.doGet(request, response);

    System.out.println("Out: "+stringWriter.toString());

  }

每次 mvn 测试运行后,我的本地存储客户列表都在增长。

imac:~ Felix$ ls -l /Users/Felix/Development/workspace-clb/clb-appEngine/./WEB-  INF/appengine-generated/
total 32
-rw-r--r--  1 Felix  Felix     81 22 Okt 22:00 datastore-indexes-auto.xml
-rw-r--r--  1 Felix  Felix  10068 22 Okt 22:00 local_db.bin
imac:~ Felix$ ls -l /Users/Felix/Development/workspace-clb/clb-appEngine/./WEB-INF/appengine-generated/
total 32
-rw-r--r--  1 Felix  Felix     81 22 Okt 22:08 datastore-indexes-auto.xml
-rw-r--r--  1 Felix  Felix  11265 22 Okt 22:08 local_db.bin`
于 2013-10-22T20:20:37.743 回答