2015-09-17 53 views
1

,這是你做了什麼如何。如果你想使用的數據存儲服務來執行JUnit測試使用ObjectifyService在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))); 
} 

我曾嘗試用物化

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

回答

1

您可能忘記添加所需的依賴項。

這是你的問題的工作測試:

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' 
} 
+0

我收到2個錯誤。無法解析'ObjectifyService.begin()'中的方法begin()和檢索食物的代碼中的方法'doTest',我收到錯誤**無法訪問com.google.appengine.api.datastore.QueryResultIterable **放置'.... ofy()。load().....' –

+0

@gikarasojo你正在檢查我分享的所有依賴關係嗎? – Devrim

0

我注意到,你沒有在你設置以下行()

Closable session; 
session = ObjectifyService.begin() 

你還需要這是你的助手了。 tearDown()在您的tearDown()中:

session.close();