0
今天我試着寫一個HttpServlet的測試用例。在web.xml中,我定義了一個具有參數名稱和參數值的init參數。我使用了JUnit TestCase。TestSuite for HttpServlet
通過調用這行代碼:
public void init() throws ServletException {
downloadPath = getServletContext().getRealPath(getInitParameter("downloadPath"));
}
,並會拋出異常:
java.lang.IllegalStateException: ServletConfig has not been initialized
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:185)
at com.example.servlet.DownloadServlet.init(DownloadServlet.java:30)
我想這是因爲東西是不是在網絡容器中運行。令我驚訝的是我無法找到支持HttpServlet的測試套件。我發現Apache仙人掌的StrutsTestCase或ServletTestCase。
我可以用這個測試套件編寫servlet測試用例嗎?我可以使用Mockito來包裝ServletConfig嗎?