这是我的ServletUnit代码:
File webXml = new File(path/to/web.xml);
ServletRunner sr = new ServletRunner(webXml);
ServletUnitClient sc = sr.newClient();
PostMethodWebRequest request = new PostMethodWebRequest("http://localhost/responseHtmlView.jsp");
sc.getResponse(request);
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>org.apache.jsp.responseHtmlView_jsp</servlet-name>
<servlet-class>org.apache.jsp.responseHtmlView_jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>org.apache.jsp.responseHtmlView_jsp</servlet-name>
<url-pattern>/responseHtmlView.jsp</url-pattern>
</servlet-mapping>
</web-app>
responseHtmlView.jsp
<html>
<head><title>First JSP</title></head>
<body>
<p>Test text</p>
</body>
</html>
我使用Tomcat 7和Ant(jasper2任务)预编译jsp,在代码执行期间我得到异常:
java.lang.NullPointerException
at org.apache.jsp.responseHtmlView_jsp._jspInit(responseHtmlView_jsp.java:32)
at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:49) at com.meterware.servletunit.WebApplication$ServletConfiguration.getServlet(WebApplication.java:587)
at com.meterware.servletunit.WebApplication$ServletRequestImpl.getServlet(WebApplication.java:775)
at com.meterware.servletunit.InvocationContextImpl$ExecutionContext.getServlet(InvocationContextImpl.java:248)
at com.meterware.servletunit.InvocationContextImpl.getServlet(InvocationContextImpl.java:86)
at com.meterware.servletunit.InvocationContextImpl.service(InvocationContextImpl.java:76)
at com.meterware.servletunit.ServletUnitClient.newResponse(ServletUnitClient.java:126)
at com.meterware.httpunit.WebClient.createResponse(WebClient.java:647) at com.meterware.httpunit.WebWindow.getResource(WebWindow.java:220)
at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:181)
at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:158)
at com.meterware.httpunit.WebClient.getResponse(WebClient.java:122)
...
这一行:
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
将_jspxFactory初始化为:
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory(); // == null
这是一个错误,如下所示: https://bz.apache.org/bugzilla/show_bug.cgi?id=40820
但我无法找到解决方法,因为关于我正在尝试做什么的信息不多。我正在尝试获取jsp servlet响应,从中获取HTML正文,然后能够将其与另一个进行比较以进行差异检查。这个动作是一个功能测试,所以我不想运行一个完整的服务器,我不想搞任何像Selenium那样重的东西。