2011-07-11 12 views
2

GAEを初めて使用し、いくつかのJUnitテストをセットアップしようとしました。次の行は、ローカルデータストアにエンティティを追加するために使用されるJUnit Google App Engineデベロッパーサーバーでテストする

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

Googleが提供するこの例では。しかし、私はJDOを使用していて、自分自身のPOJO(たとえばCars)を1つ保持したいが、Carsはこのメソッドが必要とするエンティティタイプではない。これを達成するために使用できる方法やサービスがありますか?

+0

動作します。..客観-のAppEngineを使用することができますか? –

+0

@citizen com.google.appengine.api.datastore.EntityはFinalとマークされています。 – user76162

答えて

2

多分あなたは、あなたのPOJOだけでエンティティを拡張することができます例

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("[email protected]","actividad1"); 
     ofy().save().entity(voto1).now(); 
     assertEquals(1, ds.prepare(new Query("Voto")).countEntities(withLimit(10)));    

} 
} 

のためにそれは完全に