Teedaのユーティリティを作ったのですが、そのテストケース作成で四苦八苦。
以下はその時に学んだこと等のまとめです。
ちなみにそのユーティリティとは、
LabelHelperのように、ロケールを判別して、
それに対応したプロパティファイルからメッセージを取得するというものです。


【作成ユーティリティ】
パッケージ:jp.co.hoge.util
クラス:FugaUtil.java

package jp.co.hoge.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import org.seasar.framework.util.ResourceUtil;

public class LocaleUtil {

    public static final String RESOURCE_NAME = "message";
    public static final String RESOURCE_SUFFIX = ".properties";
    public static final String DEFAULT_RESOURCE_FILE = RESOURCE_NAME + RESOURCE_SUFFIX;

    //プロパティ
    private Properties properties = null;
    //デフォルトプロパティ
    private Properties defaultProperties = null;
    //初期化フラグ
    private boolean init = false;
    //言語
    private String language = null;
	
    /**
     * ロケールメッセージを取得します
     * @param キー
     * @return メッセージ
     */
    public String get(String key) {
        if (key == null) {
            return null;
        }
        if (!init) {
            init();
        }
        String lang = getLanguage();
        reloadProperties(lang);

        //プロパティファイルが取得できている場合は、プロパティファイルから値を取得
        Object value = (properties != null) ? properties.get(key) : null;
        //値が取得できていない場合は、デフォルトのプロパティファイルから取得
        if (value == null ) value = defaultProperties.get(key);

        return (String)value;
    }
	
    /**
     * 初期処理
     * デフォルトプロパティの設定を行う。
     */
    public void init() {
    if (defaultProperties == null) {
        //デフォルトのプロパティファイルを取得
        defaultProperties = loadProperties(DEFAULT_RESOURCE_FILE);
    }
        init = true;
    }

    /**
     * 指定された言語のプロパティファイルを取得します。
     * @param 言語 ex.)en,ja,zh,…
     */
    private void reloadProperties(String lang) {
        if (lang == null || lang.equals(language) ) return;
		
        String fileName = RESOURCE_NAME + "_" + lang.toLowerCase() + RESOURCE_SUFFIX;
        // プロパティファイルを取得
        properties = loadProperties(fileName);
        //言語をセーブ
        language = lang;
    }
	
    /**
     * 言語を取得します。
     * @return 言語 ex.)en,ja,zh,…
     */
    private String getLanguage() {
        String language = null;
        final FacesContext context = FacesContext.getCurrentInstance();
        if (context != null){
            final UIViewRoot viewRoot = context.getViewRoot();
            if (viewRoot != null) {
                final Locale locale = viewRoot.getLocale();
                final String lang = locale.getLanguage();
                if (lang != null) {
                    language = lang.toLowerCase();
                }
            }
        }
        return language;
    }

    /**
     * 指定されたプロパティファイルをロードします。
     * @param ファイル名
     * @return ロードしたプロパティ(ファイルが存在しない場合や、IOExceptionが発生した場合はnullを返す)
     */
    private Properties loadProperties(String fileName) {
        InputStream is = ResourceUtil.getResourceAsStreamNoException(fileName);
        Properties properties = null;
        if (is != null) {
            properties = new Properties();
            try {
                properties.load(is);
            } catch(IOException ioe) {
                properties = null;
            }
        }
        return properties;
    }

}


【作成テストケース】

package jp.co.hoge.util;

import java.util.Locale;

import javax.faces.component.UIViewRoot;

import jp.co.hoge.util.FugaUtil;

import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
import org.seasar.framework.convention.NamingConvention;
import org.seasar.framework.convention.impl.NamingConventionImpl;
import org.seasar.framework.util.ClassUtil;
import org.seasar.teeda.core.mock.MockFacesContext;
import org.seasar.teeda.core.unit.TeedaTestCase;

public class FugaUtilTest extends TeedaTestCase {
	
    public void testGetJapanese() throws Exception {

        //コンポーネントの登録
        register(jp.co.hoge.util.FugaUtil.class, "fugaUtil");

        NamingConventionImpl nc = (NamingConventionImpl)getContainer().getComponent(NamingConvention.class);
        nc.addRootPackageName(ClassUtil.getPackageName(getClass()));

        MockFacesContext mfc = getFacesContext();
        UIViewRoot viewRoot = mfc.getViewRoot();
        //ロケールの設定
        viewRoot.setLocale(new Locale("ja","",""));
        
        S2Container container = SingletonS2ContainerFactory.getContainer();
        FugaUtil fugaUtil = (FugaUtil)container.getComponent(FugaUtil.class);
        assertEquals("メッセージ1", fugaUtil.get("MESSAGE_1"));
        assertEquals("メッセージ2", fugaUtil.get("MESSAGE_2"));
    }
	
}
  1. TeedaのテストケースはTeedaTestCaseを継承して作る。
  2. テスト対象のコンポーネントをS2コンテナに登録する。
    registerメソッドはS2FrameworkTestCaseで提供されているメソッドです。
  3. 登録したコンポーネントインスタンスをS2コンテナから取得し使用する。


う〜ん、かなり説明不足ですが、とりあえずはこんなもので。。。

ブログもJavaTeedaも初心者ですが、精進していきます!