ServletTestCase の原則/ServletTestCase Principles

Last update : July 6 2002
Doc for : v1.3

Cactusについて
  • Cactus とは
  • ニュース
  • 変更履歴
  • 特徴/開発状況
  • 目標
  • ロードマップ/ToDo
  • 協力者
  • 協力者募集
  • Cactus ユーザ
  • テスト済環境 ...
  • ライセンス


  • ダウンロード
  • ダウンロード


  • ドキュメント
  • Cactus の仕組み
  • さぁ始めよう
  • モック対コンテナ
  • Javadocs
  • └Javadocs
  • よくある質問


  • Howto ガイド
  • クラスパス Howto
  • 設定 Howto
  • アップグレードHowto
  • テストケース Howto
  • セキュリティHowto
  • Ant Howto
  • HttpUnit Howto
  • サンプル Howto
  • EJB Howto
  • IDE Howto
  • JUnitEE Howto


  • サポート
  • Bug DB
  • メーリングリスト


  • その他
  • 名前の由来
  • ロゴコンテスト
  • 参考文献
  • アクセス状況
  • └WebAlizer


  • 開発者向け
  • CVS
  • コード規約
  • ビルドの結果


  • いつ使うのか/When to use ?

    Your test case class should extend ServletTestCase whenever you are unit testing :

    次のようなものを単体テストするときには、 いつでもテストケースクラスは ServletTestCase を拡張しなければなりません :

    • Servlets,
    • サーブレット
    • Any java code that uses Servlet API objects (HttpServletRequest, ...)
    • (HttpServletRequestなど) Servlet API オブジェクトを使った、どんな java コードでも

    提供される暗黙オブジェクト/Provided implicit objects

    Cactus automatically initializes the implicit objects for you and they are made available to your setUp(), testXXX() and tearDown() methods as instance variables of the ServletTestCase class (and thus as instance variables of your test case class as it extends ServletTestCase).

    Cactus は自動的に開発者用に暗黙オブジェクトを初期化し、 ServletTestCase クラスのインスタンス変数として、 (従って、ServletTestCaseを拡張するような 自分のテストケースクラスのインスタンス変数として) 自分のsetUp()testXXX()、および tearDown() メソッドで利用可能になります。

    Note You may ask yourself how Cactus initializes these objects. The mechanism is described in the How it works guide.

    Note Cactus が、これらのオブジェクトをどうやって初期化するのか気になるかもしれません。 その機構はどのように動くか のガイドに詳しく書かれています。

    The provided implicit objects are :

    提供される暗黙オブジェクトは次の通りです :

    request

    Instance variable name   request  
    Class name   org.apache.cactus.server.HttpServletRequestWrapper , which inherits from javax.servlet.http.HttpServletRequest  
    インスタンス変数名   request  
    クラス名   javax.servlet.http.HttpServletRequest を継承した org.apache.cactus.server.HttpServletRequestWrapper  

    Cactus wraps methods of the original HTTP request in order to return the HTTP related parameters set up in the beginXXX() method. Thus, you will be able to pass this request object to your code to test and set the needed parameter in the beginXXX() method.

    beginXXX() メソッドでセットアップされる HTTP に関連するパラメーターを返すために、Cactus はオリジナルの HTTP リクエストのメソッドをラップします。 これにより、テスト用にこのリクエストオブジェクトを自分のコードに渡し、 beginXXX() メソッドのおいて、必要なパラメーターを設定するのです。

    For example, if your code under test calls getCookies() on the request object that you have passed to it, it will return the cookies that you have added to the HTTP request in beginXXX() by calling the WebRequest.addCookie() method.

    例えば、自分が渡したリクエストオブジェクト上で、 自分のテスト中のコードが getCookies() を呼び出した時、 WebRequest.addCookie() メソッドを呼び出すことにより、 beginXXX() 中で HTTP リクエストに加えたクッキーを返します。

    See the javadoc for the org.apache.cactus.WebRequest and org.apache.cactus.server.HttpServletRequestWrapper classes for all details. You should also look at the samples provided in the Cactus distribution.

    詳しくは、 org.apache.cactus.WebRequest および org.apache.cactus.server.HttpServletRequestWrapper クラスの javadoc をご覧下さい。 また、Cactus のディストリビューションで提供される例題もご覧ください。


    response

    Instance variable name   response  
    Class name   javax.servlet.http.HttpServletResponse  
    インスタンス変数名   response  
    クラス名   javax.servlet.http.HttpServletResponse  

    Note Cactus does not wrap the response.

    Note Cactus は response をラップしません。


    config

    Instance variable name   config  
    Class name   org.apache.cactus.server.ServletConfigWrapper , which inherits from javax.servlet.ServletConfig  
    インスタンス変数名   config  
    クラス名   javax.servlet.ServletConfig を継承した org.apache.cactus.server.ServletConfigWrappern  

    Cactus wraps the original Servlet Config for two reasons :

    次の 2 つの理由により Cactus はオリジナルの Servlet Config をラップします :

    • In order to provide additional methods. For example, it is possible to initialise parameters without setting them in web.xml, etc...,
    • 追加メソッドを提供するため。 例えば、web.xml で設定しなくても、 パラメーターを初期化できるようになる、、、などです。
    • So that it can return a wrapped Servlet Context instead of the original one. This is because the Servlet Context is used to perform forwards and includes and we need to pass to these methods the original request and response. As we have wrapped the request, we need to wrap the Servlet Context to pass the original request (and not the wrapped one).
    • オリジナルの代りにラップされた Servlet Context を返せるようにするため。 何故なら、 Servlet Context はページ転送や挿入を行うために使われ、 これらのメソッドにオリジナルのリクエストをレスポンスを渡す必要があるからです。 我々はリクエストをラップしたので、 (ラップされたものではない)オリジナルのリクエストを渡すために、 Servlet Context をラップする必要があります。

    The config implicit object will contain all initialisation parameters defined in web.xml under the Servlet Redirector servlet definition.

    config 暗黙オブジェクトは Servlet Redirector 定義の元で、 web.xml で定義された全ての初期化パラメーターを持っています。

    See the javadoc for the org.apache.cactus.server.ServletConfigWrapper class for all details. You should also look at the samples provided in the Cactus distribution.

    詳細については、 org.apache.cactus.server.ServletConfigWrapper クラスの javadoc をご覧ください。 また、Cactus ディストリビューションで提供される例題もご覧ください。


    session

    Instance variable name   session  
    Class name   javax.servlet.http.HttpSession  
    インスタンス変数名   session  
    クラス名   javax.servlet.http.HttpSession  

    Note Cactus does not wrap the response.

    Note Cactus は response をラップしません

    By default, Cactus always creates an HTRP session for your test case. It is possible to tell it not to do so by calling the WebRequest.setAutomaticSession(false) method (it is true by default). This could be useful for cases where your code under test verifies branches when "request.getSession(false)" is null.

    デフォルトでは、常に Cactus は自分のテストケース用に HTTP セッションを生成します。 WebRequest.setAutomaticSession(false) メソッドを呼び出すことにより、 そうしないよう、指定することができます。 (デフォルトでは trueです。) "request.getSession(false)" が null であるとき、 テスト中のコードがブランチを検証するようなケースの場合便利かもしれません。



    Tip と Trick/Tips and Tricks

    パラメーターの初期化/Parameter initialisation

    If your code under test make use of any of the servlet methods inherited from javax.servlet.GenericServlet (these are the log(), getServletConfig(), ... methods), then you need to call the init(ServletConfig) method of your servlet to initialise its internal ServletConfig object.

    自分のテスト中のコードが javax.servlet.GenericServlet から継承したいずれかのサーブレットメソッド (log()getServletConfig()メソッドなど) を使う場合、 ServletConfig 内部オブジェクトを初期化するために、 自分のサーブレットのinit(ServletConfig)メソッドを呼び出さなければなりません。

    For example :

    例 :

    public void testXXX()
    {
        MyServletToTest servlet = new MyServletToTest();
        servlet.init(config);
    
        // Call a method to test that uses a method inherited from Generic Servlet
        // Generic Servlet から継承したメソッドを利用するテストするためのメソッド呼び出し
        servlet.someMethodToTest();
    
    [...]
    }
    

    See the samples provided as part of the Cactus distribution.

    Cactus ディストリビューションの一部として提供されるサンプルをご覧ください。



    例題/Sample

    This is a very basic sample intended to give you a flavour of Servlet unit testing. Check the distribution samples for extensive examples.

    これは、サーブレットの単体テストの雰囲気をわかってもらうためのとても基本的な例題です。 拡張した例題については、ディストリビューションのサンプルをご覧ください。

    Note This example is for Cactus 1.2 and above as it uses the new WebRequest and WebResponse objects.

    Note この例題は、 新しいWebRequest および WebResponseオブジェクトを使っているので、 Cactus 1.2 以上のためのものです。

    public void beginXXX(WebRequest theRequest)
    {
        // Set up HTTP related parameters
        // HTTP関連のパラメーターを設定します
        theRequest.setURL("jakarta.apache.org", "/mywebapp", "/test/test.jsp",
            null, null);
        theRequest.addCookie("cookiename", "cookievalue");
    }
    
    public void testXXX()
    {
        MyServletToTest servlet = new MyServletToTest();
        servlet.init(config);
    
        // Call method to test
        // テストのためのメソッドを呼び出します
        servlet.methodToTest();
    
        // Perform some server side asserts
        // 幾つかのサーバー側のアサーションを行います
        assertEquals("someValue", session.getAttribute("someAttribute"));
        assertEquals("jakarta.apache.org", request.getServerName());
    }
    
    public void endXXX(WebResponse theResponse)
    {
        // Asserts the returned HTTP response
        // 返される HTTP レスポンスのためのアサーション
    
        Cookie cookie = theResponse.getCookie("someCookie");
        assertEquals("someValue2", cookie.getValue());
    
        assertEquals("some content here", theResponse.getText());
    }
    



    Copyright © 2000-2002 The Apache Software Foundation. All Rights Reserved.