JUnit Testing with Google App Engine dev server
I'm new to GAE and trying to setup a few JUnit tests. In this example provided by Google:
public class LocalDatastoreTest {
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
// run this test twice to prove we're not leaking any state across tests
private void doTest() {
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
ds.put(new Entity("yam"));
ds.put(new Entity("yam"));
assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
}
@Test
public void testInsert1() {
doTest();
}
@Test
public void testInsert2() {
doTest();
}
}
the following line is used to add an Entity to the local datastore:
ds.put(new Entity("yam"));
That works just fine for me. However, I'm using 开发者_StackOverflowJDO and want to persist one my own POJOs (e.g. Cars) but Cars is not of type Entity, which is what this method requires. Is there a different method or service I can use to accomplish this?
maybe you could use objectify-appengine.. for example
package com.intranet.entity;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
@Entity
public class Voto {
@Id Long id;
@Index String email;
@Index String actividad;
public Voto(){}
public Voto(String email, String actividad) {
this.email = email;
this.actividad = actividad;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getActividad() {
return actividad;
}
public void setActividad(String actividad) {
this.actividad = actividad;
}
}
TEST
public class VotoTest {
private final static LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalDatastoreServiceTestConfig());
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void testEmbedded(){
DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Voto voto1 = new Voto("test@localhost","actividad1");
ofy().save().entity(voto1).now();
assertEquals(1, ds.prepare(new Query("Voto")).countEntities(withLimit(10)));
}
}
It works perfectly
精彩评论