1

ServletUnit を使用してサーブレットの単体テストを計画していましたが、いくつかの問題に遭遇しました:
- 出発点として、ServletRunner オブジェクトを作成することになっています。コンストラクターの 1 つは、web.xml ファイルを含む File オブジェクトを想定しています。web.xml ファイルのフル パスを指定しましたが、指定されたパスが無視され、最上位のフォルダーが検索されます。コード スニペットとエラー メッセージは次のとおりです。

コードスニペット

    ServletRunner sr = new ServletRunner(new File("* C:/eclipse-workspaces/pocs/lms-csd/src/main/webapp/WEB-INF/web.xml*")); 
ServletUnitClient sc = sr.newClient(); 
 WebRequest request = new PostMethodWebRequest("path to be specified" ); request.setParameter( "userId", "test" );
 request.setParameter( "password", "csd" );
  WebResponse response = sc.getResponse(request);
  String text = response.getText();

Assert.assertTrue(text.contains("Welcome to Leave Management System"));

スタックトレース

    com.meterware.httpunit.HttpInternalErrorException:
 Error on HTTP request: 500 org.apache.jasper.JasperException: java.io.FileNotFoundException: * C:\eclipse-workspaces\pocs\lms-csd\WEB-INF\web.xml* 
(The system cannot find the path specified)

[http://localhost/login] - システムがフォルダ構造を常に .../WEB-INF/web.xml と見なすのはなぜですか。私はmavenプロジェクトであり、このように適応するためにプロジェクトの構造を変更したくありません。指定したフォルダから読み取るように ServletRunner クラスを作成するにはどうすればよいですか? - サーブレット コードでは、次のコードを使用します。

 String result = null if (someCondition) result = "/welcome.jsp"; } else { logger.warn("Password Validation failed"); request.setAttribute("failedlogin", new Boolean(true)); result = "/index.jsp"; } } RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(result); requestDispatcher.forward(request, response); 

jsp ファイルは .../src/main/webapp/ フォルダーに存在しますが、やはり ServletUnit は、welcome.jsp がルート フォルダーにあることを想定しています。もう一度、ServletUnit にターゲット フォルダの場所を通知するにはどうすればよいでしょうか。

よろしくお願いします。

よろしく M.SuriNaidu

4

1 に答える 1

0

これは私がするようなことです。これは、私のサーブレット テストの基本クラスの複製です。この場合、ソース ツリーに存在する web.xml ファイルの相対パスを渡します。これらのテストは、ant と eclipse から実行します。

abstract public class ServletTestCase {

    protected ServletRunner       m_runner;
    protected ServletUnitClient   m_client;
    protected String              m_userAgent = "something/1.0";

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

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

    protected void initHttpUnit() throws IOException, SAXException {
        shutdownHttpUnit();

        // We are expecting UTF-8 character handling in URLs, make it the default
        HttpUnitOptions.setDefaultCharacterSet("UTF-8");

        // Find the servlet's web.xml file and use it to init servletunit
        File file = new File("tests/web.xml"));
        m_runner = new ServletRunner(file);
        m_client = m_runner.newClient();
        m_client.getClientProperties().setUserAgent(m_userAgent);
    }

    protected void shutdownHttpUnit() {
        if (m_runner != null) {
            m_runner.shutDown();
        }
        m_client = null;
        m_runner = null;
    }
}
于 2013-02-23T04:59:12.217 に答える