サーブレットを作成しました。サーブレット クラスの機能をテストしたいので、サーブレットのテスト ケースを作成しました。しかし、次の例外がスローされます。
com.meterware.httpunit.HttpException: Error on HTTP request: 405 Method Not Allowed [http://localhost:8090/getPrintSettings]
at com.meterware.httpunit.WebClient.validateHeaders(WebClient.java:623)
at com.meterware.httpunit.WebClient.updateClient(WebClient.java:500)
at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:192)
at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:183)
at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:158)
at com.meterware.httpunit.WebClient.getResponse(WebClient.java:122)
at com.vxoom.qit.basic.GetPrintSettingsTest.testGetForm(GetPrintSettingsTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
これは私のサーブレットクラスです:
public class GetPrintSettings extends HttpServlet {
private Logger log;
DB db;
Connection connection;
public void init() throws ServletException { //initialise servlet
log = Logger.getRootLogger();
db = new DB();
connection = db.connect();
super.init();
}
public void destroy() { //destroy servlet
try {
connection.close();
}
catch (SQLException e) {
log.error("AddAddress: SQL Exception: "+e.getMessage());
e.printStackTrace();
}
super.destroy();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
int userType = Integer.parseInt(request.getParameter("user"));
response.setContentType("application/json");
PrintWriter out = response.getWriter();
//Get print settings from JavaClient
String json = getPrintSettings();
if(json == null) {
out.print("{success:false, msg:'Could not send request. Please try again'}");
out.close();
return;
}
//Send the reply to the web client
out.print(json);
out.close();
}
}
これは私のサーブテストケースです
public class GetPrintSettingsTest extends TestCase {
public static void main( String args[] ) {
junit.textui.TestRunner.run(GetPrintSettingsTest.class );
}
public void testGetForm() throws Exception {
WebConversation web = new WebConversation();
WebRequest request = new GetMethodWebRequest("http://localhost:8090/getPrintSettings" );
WebResponse response = web.getResponse(request);
request.setParameter("user", "1");
response = web.getResponse( request );
}
}
私は httpunit1.7 を使用しました 助けてください ...
前もって感謝します :)