SAStruts で test をするの巻き

こんな感じだろうとおもってやったら出来た。
すげー簡単。


package sabbs.action;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import org.junit.Before;
import org.junit.Test;
import org.seasar.extension.jdbc.JdbcManager;
import org.seasar.extension.unit.S2TestCase;
import org.seasar.framework.container.SingletonS2Container;
import org.seasar.framework.container.factory.SingletonS2ContainerFactory;

import sabbs.dto.StateListDto;
import sabbs.entity.Genre;
import sabbs.form.GenreForm;
import sabbs.service.BoardService;
import sabbs.service.GenreService;
import sabbs.service.SeqGenreIdService;

public class GenreActionTest extends S2TestCase {
protected GenreService genreService;
protected BoardService boardService;
protected SeqGenreIdService seqGenreIdService;
protected GenreForm genreForm;
protected StateListDto stateListDto;
protected HttpSession session;

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

@Test
public void testIndex() {
SingletonS2ContainerFactory.init();
try {
JdbcManager jdbcManager = SingletonS2Container
.getComponent(JdbcManager.class);
UserTransaction ut = SingletonS2Container
.getComponent(UserTransaction.class);
ut.begin();
try {
GenreAction action = new GenreAction();
//メンバ設定
action.genreService = genreService;
action.genreForm = genreForm;
// テスト実施
String rtn = action.index();
// 判定
assertNotNull("遷移先が null.", rtn);
assertEquals("list.jsp へ遷移されない", "list.jsp", rtn);
assertFalse(action.genreItems.size() == 0);
} finally {
ut.rollback();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
SingletonS2ContainerFactory.destroy();
}
}

@Test
public void testShow() {
SingletonS2ContainerFactory.init();
try {
JdbcManager jdbcManager = SingletonS2Container
.getComponent(JdbcManager.class);
UserTransaction ut = SingletonS2Container
.getComponent(UserTransaction.class);
ut.begin();
try {
GenreAction action = new GenreAction();
//メンバ設定
action.genreService = genreService;
action.boardService = boardService;
action.genreForm = genreForm;
action.genreForm.genreId = "1";
// テスト実施
String rtn = action.show();
// 判定
assertNotNull("遷移先がnull.", rtn);
assertEquals("show.jsp へ遷移されない", "show.jsp", rtn);
assertFalse(action.boardItems.size() == 0);
} finally {
ut.rollback();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
SingletonS2ContainerFactory.destroy();
}
}

@Test
public void testInit() {
SingletonS2ContainerFactory.init();
try {
JdbcManager jdbcManager = SingletonS2Container
.getComponent(JdbcManager.class);
UserTransaction ut = SingletonS2Container
.getComponent(UserTransaction.class);
ut.begin();
try {
GenreAction action = new GenreAction();
//メンバ設定
action.genreForm = genreForm;
action.stateListDto = stateListDto;
action.session = session;
// テスト実施
String rtn = action.init();
// 判定
assertNotNull("遷移先がnull.", rtn);
assertNotNull("セッションがセットされていない.", session);
assertEquals("create へリダイレクトされない", "create?redirect=true", rtn);
} finally {
ut.rollback();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
SingletonS2ContainerFactory.destroy();
}
}

@Test
public void testInsert() {
SingletonS2ContainerFactory.init();
try {
JdbcManager jdbcManager = SingletonS2Container
.getComponent(JdbcManager.class);
UserTransaction ut = SingletonS2Container
.getComponent(UserTransaction.class);
ut.begin();
try {
GenreAction action = new GenreAction();
//メンバ設定
action.genreForm = genreForm;
action.genreForm.name = "ジャンル作成テスト";
action.genreForm.state = "true";
action.genreForm.ord = "1";
action.genreService = genreService;
action.seqGenreIdService = seqGenreIdService;
// テスト実施
String rtn = action.insert();
// 判定
assertNotNull("遷移先がnull.", rtn);
assertEquals("/genre へリダイレクトされない", "/genre/?redirect=true", rtn);
} finally {
ut.rollback();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
SingletonS2ContainerFactory.destroy();
}
}
}