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