アクションのテスト

アクションの単体テストはCubbyTestCase を使用します。 SeasarのS2Unitの機能を全て使用できるので SeasarのS2Unitのドキュメント も合わせてご覧ください。

  1. CubbyTestCaseを継承して、XXXXTest作成します。
  2. テスト対象のアクションをフィールドに用意します。これはアクション実行後に結果を取得するためです。
  3. アクションのメソッドごとにテストメソッドを書きます。メソッド名はtestから始めます。
  4. テストメソッドの先頭で、パラメータのセットなど初期化処理を行います。
  5. CubbyTestCase#processAction にリクエストのパスを指定して、アクションメソッドを実行します。CoolURI(パラメータ付きのURI)の場合、パスにパラメータを含んで実行します(TodoActionTest#)。
  6. 戻り値をCubbyTestCase#processAction で結果のActionResultの型とパスをチェックします。パスのチェックはForwardとRedirectの場合のみ行われます。
  7. 必要に応じてアクションクラスのフィールドやリクエストやセッションの値をチェックします。

    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);
            }
    }