java - 如何在 Junit 测试中使用 ObjectifyService

标签 java google-app-engine junit objectify

如果您想使用数据存储服务来执行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 来实现此类测试?

最佳答案

您可能忘记添加所需的依赖项。

这是对您的问题的有效测试:

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.cache.AsyncCacheFilter;
import com.googlecode.objectify.util.Closeable;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static com.piyasatakip.backend.OfyService.ofy;
import static junit.framework.Assert.assertNotNull;

/**
 * Created by devrimtuncer on 27/03/16.
 */
public class ObjectifyServiceTest {


    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

    protected Closeable session;

    @BeforeClass
    public static void setUpBeforeClass() {
        // Reset the Factory so that all translators work properly.
        ObjectifyService.setFactory(new ObjectifyFactory());
        ObjectifyService.register(Food.class);
    }

    @Before
    public void setUp() {
        this.session = ObjectifyService.begin();
        this.helper.setUp();
    }

    @After
    public void tearDown() {
        AsyncCacheFilter.complete();
        this.session.close();
        this.helper.tearDown();
    }

    @Test
    public void doTest() {
        Food food = new Food("yam");

        // 1) save food to data store
        ofy().save().entity(food).now();

        // 2) retrieve food from data store
        Food foodRetrieved = ofy().load().type(Food.class).filter("foodtype", "yam").first().now();

        assertNotNull(foodRetrieved);
    }

    @Entity
    private class Food {
        @Id
        Long id;

        @Index
        String foodtype;

        public Food(String food) {
            foodtype = food;
        }
    }
}

不要忘记添加所需的依赖项。

即 build.gradle 中的依赖项:

dependencies {
    appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.34'
    compile 'com.google.appengine:appengine-endpoints:1.9.34'
    compile 'com.google.appengine:appengine-endpoints-deps:1.9.34'
    compile 'com.google.appengine:appengine-api-1.0-sdk:1.9.34'

    compile 'com.googlecode.objectify:objectify:5.1.12'

    compile 'javax.servlet:servlet-api:2.5'        
    compile 'org.jsoup:jsoup:1.7.2'

    testCompile 'com.google.appengine:appengine-testing:1.9.34'
    testCompile 'com.google.appengine:appengine-api-stubs:1.9.34'
    testCompile 'junit:junit:4.4'
}

关于java - 如何在 Junit 测试中使用 ObjectifyService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628124/

相关文章:

java - 无法从以下位置获取标识符

java - 将 NetBeans IDE 的数据库管理器与 UCanAccess JDBC 驱动程序一起使用

java - 无法读取 StateBackend Flink 的 _metadata

python - Excel 文件未显示在 Google 云端硬盘中

python - Google App Engine 中的函数调用 - Python

java - GAE-JAVA-JDO 如何构建解码后的实体 key 面包屑

java - 使用 Maven 在 Eclipse 中运行单个 JUnit4 测试

java - 处理线程中数据库连接丢失 - Java

java - java异常的catch block 中是否会捕获断言错误?

java - 如何避免重复的 JUnit 测试