使用Google App Engine开发服务器进行JUnit测试

时间:2011-07-11 19:26:50

标签: google-app-engine

我是GAE的新手并尝试设置一些JUnit测试。在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();
}

}

以下行用于将实体添加到本地数据存储区:

ds.put(new Entity("yam"));

这对我来说很好。但是,我正在使用JDO并且希望保留一个我自己的POJO(例如Cars),但是Cars不是Entity类型,这是该方法所需要的。我可以使用不同的方法或服务来实现这一目标吗?

1 个答案:

答案 0 :(得分:2)

也许你可以使用objectify-appengine ..例如

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

 } 
 }

完美无缺