2011-07-28 75 views
0

我寫了servlet.I想測試我的servlet的init()方法的功能。但是當我爲我的servlet編寫測試用例時,servletRunnermIt給出了以下例外。另外我得到了配置和上下文對象。如果我想從這兩個對象獲取init參數,它將返回null。ServletContext.getAttributes(「Name」)返回null

com.meterware.httpunit.HttpException: Error on HTTP request: 405 HTTP method GET is not supported by this URL [http://127.0.0.1:8888/NewsletterServlet] 
    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.newscenter.server.NewsletterServletTest.testFetchNews(NewsletterServletTest.java:79) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) 
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) 
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) 
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) 
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) 
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66) 
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) 
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) 
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) 
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 
    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) 
+1

顯示您的servlet代碼和XML配置。 –

+0

異常與「null」屬性完全不同。 – BalusC

回答

0

的錯誤消息表明你在你的servlet的doGet()實施已經一個

super.doGet(request, response); 

。去掉它。

HttpServlet類基於模板方法模式,其中所有未覆蓋的doXxx()方法發送指示該方法不受支持的HTTP 405錯誤。當你重寫它時,它不會發送HTTP 405錯誤,而是發送特定的overriden方法產生的任何結果。但是,無論如何,當你調用super方法時,仍然會得到405。