使用Httpunit和Servlet单元测试包含servlet中jstl代码的jsp页面

时间:2013-09-17 06:52:44

标签: jsp unit-testing jstl http-unit servletunit

我正在尝试使用httpunit servlet runner测试以下servlet代码。除了包含jstl的jsp页面之外,每件事情都很顺利。

public class ListTenantServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;
  private static final Logger log = Logger.getLogger(ListTenantServlet.class.getName());

  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
      IOException {

    doProcess(req, res);
  }

  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
      IOException {

    doProcess(req, res);
  }

  private void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException,
      IOException {
       CompanyStatus companyStatus = TenantStatus.Paid;
       req.setAttribute("companyStatus" companyStatus);
       RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/tenantList.jsp");
       dispatcher.forward(req, res);
  }

如果我尝试测试上面的代码,它会给出以下异常,但servlet在本地服务器上运行正常。

HTTP请求时出错:500 org.apache.jasper.JasperException:无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:http://java.sun.com/jsp/jstl/core [http:// localhost / contextParam / tenantmgr / listTenant]

注意:我的ListTenant.jsp有jstl代码,如果删除jstl代码,测试工作正常。

请帮助我让它发挥作用。

1 个答案:

答案 0 :(得分:0)

有点晚了,但是,您需要将一些jar可用于servlet单元。起初,它大部分时间都没有。一个很好的解决方案,它只是从本地服务器复制相同的罐子,例如。如果你使用tomcat TOMCAT_HOME / lib / * .jar。

我使用ServletUnit + Spring + Maven自动进行JSP测试并添加一些依赖项,看看我的答案here