【发布时间】:2015-12-14 04:37:20
【问题描述】:
如果你想使用数据存储服务来执行 junit 测试,这就是你要做的
LocalServiceTestHelper helper =
new LocalServiceTestHelper(new LocalMemcacheServiceTestConfig(),new LocalDatastoreServiceTestConfig());
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void testInsert1() {
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)));
}
我已经使用 objectify 尝试了上述测试
Public class myofyTest{
@Entity
private class Food{
@id Long id;
String foodtype;
public Food(String food){
foodtype = food ;
}
}
static{
ObjectifyService.register(Food.class);
}
LocalServiceTestHelper helper = new LocalServiceTestHelper
(new LocalMemcacheServiceTestConfig(),new LocalDatastoreServiceTestConfig());
@Before
public void setUp() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void testInsert1() {
Food food = new Food("yam");
ofy().save().entities(food).now();
}
}
但是我遇到了这种异常
java.util.ServiceConfigurationError: com.google.appengine.tools.development.
LocalRpcService: Provider com.google.appengine.api.datastore.dev.
LocalCloudDatastoreV1Service could not be instantiated
您如何使用 ObectifyService 而不是 DatastoreService 实现这种测试?
【问题讨论】:
标签: java google-app-engine junit objectify