アクションの単体テストはCubbyTestCase を使用します。 SeasarのS2Unitの機能を全て使用できるので SeasarのS2Unitのドキュメント も合わせてご覧ください。
HelloActionTest.java
package org.seasar.cubby.examples.other.web.hello; import org.seasar.cubby.action.ActionResult; import org.seasar.cubby.action.Forward; import org.seasar.cubby.unit.CubbyTestCase; public class HelloActionTest extends CubbyTestCase { // 対象のアクション private HelloAction action; // 初期化処理 protected void setUp() throws Exception { // diconファイルの読み込み include("app.dicon"); } public void testIndex() throws Exception { // アクションの実行 ActionResult result = processAction("/hello/"); // 結果のチェック assertPathEquals(Forward.class, "input.jsp", result); } public void testMessage() throws Exception { // リクエストパラメータのセット getRequest().addParameter("name", "name1"); // アクションの実行 ActionResult result = processAction("/hello/message"); // 結果のチェック assertPathEquals(Forward.class, "result.jsp", result); // 実行後のアクションの状態を確認 assertEquals("name1", action.name); } }
HelloActionTest.java
package org.seasar.cubby.examples.todo.action; import org.seasar.cubby.action.ActionResult; import org.seasar.cubby.action.Forward; import org.seasar.cubby.action.Redirect; import org.seasar.cubby.examples.RunDdlServletRequestListener; import org.seasar.cubby.unit.CubbyTestCase; public class TodoActionTest extends CubbyTestCase { private TodoAction action; protected void setUp() throws Exception { include("app.dicon"); RunDdlServletRequestListener listener = new RunDdlServletRequestListener(); listener.requestInitialized(null); } @Override protected void setUpAfterBindFields() throws Throwable { super.setUpAfterBindFields(); getRequest().addParameter("userId", "test"); getRequest().addParameter("password", "test"); // 後続のテストを実行するためにログインアクションを実行 assertPathEquals(Redirect.class, "/todo/", processAction("/todo/login/process")); } public void testShow() throws Exception { this.readXlsAllReplaceDb("TodoActionTest_PREPARE.xls"); // CoolURIの場合のテスト ActionResult result = processAction("/todo/1"); assertPathEquals(Forward.class, "show.jsp", result); assertEquals(new Integer(1), action.id); assertEquals("todo1", action.text); assertEquals("todo1 memo", action.memo); assertEquals(new Integer(1), action.todoType.getId()); assertEquals("type1", action.todoType.getName()); assertEquals("2008-01-01", action.limitDate); } }