[英]Persist data in local storage google app engine with Junit + eclipse
My setup: 我的设置:
win 7 , eclipse , GAE sdk 1.8.5 ,objectify 4 , junit4 win 7,eclipse,GAE sdk 1.8.5,objectify 4,junit4
I am trying to write Junit test cases to save two Car entities and retrieve them. 我正在尝试编写Junit测试用例,以保存两个Car实体并检索它们。 I am using objectify for it and it works just fine. 我为此使用了对象化,并且效果很好。
My problem is that I need to persist those entities across my many runs of the test cases, that means once I save using doSaveCar() in one run , in the next run , I should get back those two entities when I run doListCar(). 我的问题是我需要在多次测试用例中保留这些实体,这意味着一旦我在一次运行中使用doSaveCar()保存,在下一次运行中,我应该在运行doListCar()时取回这两个实体。 。 How do I persist local storage across my various junit test runs in eclipse environment ? 如何在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);
}
}
I keep the data from one run to the other with using objectify, junit, mockito and google-app-engine, by setting the NoStorage
flag. 我通过设置NoStorage
标志,使用objectify,junit,mockito和google-app-engine将数据从一次运行转移到另一次运行。
dsService.setNoStorage(false);
Refer to the below junit test 参考下面的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());
}
My list of locally stored customers is growing after each mvn test run. 每次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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.