我的设置:
赢得7,eclipse,GAE sdk 1.8.5,客观化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);
}
}
答案 0 :(得分: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`