2011年4月30日土曜日

Google App Engineのユニットテストのドキュメント(その4)

前回に続き、Google App Engine for Javaのテストについて
翻訳します。

ちなみに、翻訳にはKaede翻訳ツールを使っています。
もともと、こういう用途に使うために作ったものですからね。

翻訳元のドキュメントは
http://code.google.com/intl/en/appengine/docs/java/tools/localunittesting.html
です。

Writing Datastore and Memcache Tests (データストアとMemcacheのテストの記述)
以下のサンプルは、データストアサービスを使用しているテストです。
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

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(FetchOptions.Builder.withLimit(10)));
        ds.put(new Entity("yam"));
        ds.put(new Entity("yam"));
        assertEquals(2, ds.prepare(new Query("yam")).countEntities(FetchOptions.Builder.withLimit(10)));
    }

    @Test
    public void testInsert1() {
        doTest();
    }

    @Test
    public void testInsert2() {
        doTest();
    }
}
このサンプルでは、LocalServiceTestHelperは全てのローカルサービスに共通する実行環境の部分のセットアップと終了処理を行い、LocalDatastoreServiceTestConfigがローカルデータストアサービスの特定された実行環境の部分のセットアップと終了処理を行っています。javadocを参照すると、全てのデータは(定期的にディスク内にフラッシュされるのとは対照的に)メモリ内に保持され、テストの終了ごとに全てのメモリ内のデータが一掃されるようにローカルデータサービスが初期化されるということが分かるでしょう。これはデータストアのテストにおけるデフォルトの振る舞いでしかなく、意図した通りではないときには、変更することができます。

2011/5/6 訳者訂正:
doTest()メソッド内2行目:withLimit(10); → FetchOptions.Builder.withLimit(10);
doTest()メソッド内5行目:withLimit(10); → FetchOptions.Builder.withLimit(10);



Changing the example to access memcache instead of datastore (データストアの代りにmemcacheにアクセスするようにサンプルを変更)
ローカルmemcacheサービスにアクセスするテストを作成するには、先述のコードを少し変更することで対応できます。
データストアに関するクラスをインポートする代わりに、memcacheに関するクラスをインポートします。LocalServiceTestHelperについては、同じくインポートする必要があります。
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
作成するクラスの名前を変更して、LocalServiceTestHelperのインスタンスをmemcacheを指定するように変更します。
public class LocalMemcacheTest {

    private final LocalServiceTestHelper helper =
        new LocalServiceTestHelper(new LocalMemcacheServiceTestConfig());
最後に、実際にテストを実行する部分をmemcacheに関連するように変更します。
    private void doTest() {
        MemcacheService ms = MemcacheServiceFactory.getMemcacheService();
        assertFalse(ms.contains("yar"));
        ms.put("yar", "foo");
        assertTrue(ms.contains("yar"));
    }
データストアについての例では、LocalServiceTestHelperとサービスに付随するLocalServiceTestConfig(ここではLocalMemcacheServiceTestConfig)が実行環境を管理しています。



続く。

インストール不要・無料のKaede翻訳ツール
http://kaedetrans.appspot.com/

0 件のコメント:

コメントを投稿