SAStruts+S2JDBCでwebアプリ(6)

Unitテスト

J2Unit(JUnit3の拡張機能)とJ2Unit4(JUnit4向けの拡張機能)が存在する。
S2Unit

    • ActionクラスのUnitテスト
public class DeptActionTest extends S2TestCase {

	/* 自動バインディングしてくれる */
	private DeptService deptService;
	private UserDto userDto;
	private DeptForm deptForm;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		include("app.dicon");
	}

	/**
	 * indexメソッドのテスト
	 */
	public void testIndex() {
		//actionクラスのインスタンス生成
		DeptAction action = new DeptAction();
		//serviceクラスの設定
		action.deptService = deptService;
		action.userDto = userDto;

		//Excelファイルのデータ読み込み
		DataSet expect = readXls("Dept.xls");

		//実行
		String page = action.index();

		//結果比較
		assertEquals(page, "list.jsp");
		assertNotNull(action.deptItems);
		assertTrue(action.deptItems.size() > 0);
		assertEquals(expect, action.deptItems);
	}

	/**
	 * DEPTテーブルデータのExcelファイル作成
	 */
	public void testWriteExcel(){
		DataSource ds = getDataSource();
		SqlReader reader = new SqlReader(ds);
		reader.addTable("DEPT");
		DataSet result = reader.read();
		writeXls("Dept00.xls", result);
	}

	/**
	 * showメソッドのテスト
	 */
	public void testShow() {
		//actionクラスのインスタンス生成
		DeptAction action = new DeptAction();
		//serviceクラスの設定
		action.deptService = deptService;
		deptForm.id = "1";
		action.deptForm = deptForm;
		//比較データ
		DeptForm form = new DeptForm();
		form.id = "1";
		form.deptNo = "10";

		//実行
		String page = action.show();

		assertEquals("show.jsp",page);
		assertEquals(form.id,action.deptForm.id);
		assertEquals(form.deptNo,action.deptForm.deptNo);
	}

	/**
	 * mycountメソッドのテスト
	 */
	public void testMycount() {
		DeptAction ac = new DeptAction();
		ac.deptService = deptService;
		String page = ac.mycount();

		assertEquals("list.jsp",page);
		assertTrue(ac.cnt > 0);
	}

	/**
	 * mysortメソッドのテスト
	 */
	public void testMysort(){
		//actionクラスの作成
		DeptAction ac = new DeptAction();
		//mock作成
		MockInterceptor mock = new MockInterceptor();
		List<Dept> list = new ArrayList<Dept>();
		Dept d = new Dept();
		d.id = (long)1;
		d.deptNo = 100;
		list.add(d);
		//mockのメソッドの戻り値を設定
		mock.setReturnValue("getSortList", list);

		//mockセット
		ac.deptService = (DeptService)mock.createProxy(DeptService.class);
		//実行
		String page = ac.mysort();

		//テスト
		assertEquals("list.jsp",page);
		assertTrue(ac.deptItems.size() == 1);
	}

	@Override
	protected void tearDown() throws Exception {
		// TODO 自動生成されたメソッド・スタブ
		super.tearDown();
	}

}
    • ServiceクラスのUnitテスト
public class DeptServiceTest extends S2TestCase {

	private DeptService deptService;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		include("app.dicon");
	}

	/**
	 * getDeptメソッドのテスト
	 */
	public void testGetDept() {
		//実行
		Dept d = deptService.getDept();
		//比較データ取得
		DataSet ds = readXls("DeptWhere.xls");
		//比較
		assertNotNull(d);
		assertEquals(ds, d);
	}

	/**
	 * insertメソッドのテスト
	 * Txを付加することで、トランザクションが最後ロールバックされる。
	 */
	public void testInsertTx(){
		Dept entity = new Dept();
		entity.deptNo = 210;
		entity.deptName = "FFF";
		entity.loc = "Osaka";
		int cnt = deptService.insert(entity);
		assertEquals(1,cnt);
	}

	/**
	 * Excelファイル出力(target/test-classes配下に出力される)
	 */
	public void testExcelWrite(){
		DataSource ds = getDataSource();
		SqlReader reader = new SqlReader(ds);
		String sql = "SELECT * FROM DEPT WHERE ID = 1";
		reader.addSql(sql, "DEPT");
		DataSet result = reader.read();
		writeXls("DeptWhere.xls", result);
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}

}

S2Unit4


ログ出力

インタセプター
http://blog.mikuriya.biz/archives/318


まずは、log4jで出力。

log4j.category.org.seasar=DEBUG, C
log4j.additivity.org.seasar=false

log4j.category.tutorial=DEBUG, C
log4j.additivity.tutorial=false

log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p %d [%t] %m%n

log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File = C:\\Tool\\workspace\\SampleDolteng\\sample.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n



Actionクラスにて、

Logger  logger = Logger.getLogger(DeptAction.class);

logger.info("ログ出力します0000");

例外処理
例外をキャッチして、
システムエラー画面を表示する。
まず、例外クラスの作成

public class DolException extends RuntimeException {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	public DolException(Logger log,RuntimeException e){
		log.error(e.toString());
		log.error(e.getMessage());
	}
}

ActionクラスにてRuntimeEceptionをキャッチして、
作成した例外をスローする。

    @Execute(validator = false)
    public String outsql() throws DolException{
    	System.out.println("-------------------------------------------");
    	System.out.println(userDto.userName);
    	System.out.println("-------------------------------------------");
    	try{
    		deptItems = deptService.selectSqlFile();
    	}catch(RuntimeException e){
    		throw new DolException(logger,e);
    	}

    	return "list.jsp";
    }

そして、struts-config.xmlにて、
例外時の遷移画面を設定する。

    <global-exceptions>
    <exception key="errors.already"
          type="dol.exception.DolException"
          path="/WEB-INF/view/error.jsp"/>
    </global-exceptions>