使用cactus实现对servlet进行单元测试
步骤如下:
[1] 创建Web工程ServletTestDemo
在myeclipse中创建Web project,命名为ServletTestDemo
[2] 下载cactus-bin-1.8
从apache官方网站下载
[3] 导入cactus的相关包到项目的lib目录
导入后,/WebRoot/WEB-INF/lib目录结构如下所示:
|-- lib
| |-- cactus.core.framework.uberjar.javaEE.14-1.8.1.jar
| |-- cactus.integration.ant-1.8.1.jar
| |-- cactus.integration.shared.api-1.8.1.jar
| |-- commons-codec-1.4.jar
| |-- commons-httpclient-3.1.jar
| |-- commons-logging-1.1.jar
| |-- httpunit-1.6.jar
| `-- junit-3.8.2.jar
[4] 配置项目web.xml
在项目web.xml中添加如下内容:
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>value1 used for testing</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>ServletTestRunner</servlet-name> <servlet-class>org.apache.cactus.server.runner.ServletTestRunner</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletTestRunner</servlet-name>
<url-pattern>/ServletTestRunner</url-pattern>
</servlet-mapping>
[5] 创建Servlet
创建LoginServlet,代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public String getServletInfo() {
return "This is my default servlet created by Eclipse";
}
public void init() throws ServletException {
}
public void saveSession(HttpServletRequest request)
{
String testparam = request.getParameter("testparam");
request.getSession().setAttribute("testAttribute", testparam);
}
}
[6] 修改项目web.xml,添加servlet映射
在web.xml中添加如下内容:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
[7] 创建Servlet测试类
创建TestLoginServlet,代码如下:
import java.io.IOException;
import javax.servlet.ServletException;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import com.meterware.httpunit.WebResponse;
public class TestLoginServlet extends ServletTestCase {
public TestLoginServlet(String theName) {
super(theName);
}
public static Test suite() {
return new TestSuite(TestLoginServlet.class);
}
public void beginSaveSessionOk(WebRequest webRequest) {
webRequest.addParameter("testparam", "it works!");
}
public void testSaveSessionOk() {
LoginServlet servlet = new LoginServlet();
servlet.saveSession(request);
assertEquals("it works!", session.getAttribute("testAttribute"));
}
public void endSaveSessionOk(WebResponse response) {
System.out.println("end save session ok");
}
public void beginDoGet(WebRequest webRequest) {
webRequest.addParameter("testparam", "it works!");
}
public void testDoGet() {
LoginServlet servlet = new LoginServlet();
try {
servlet.doGet(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[8] 复制测试结果报表样式
下载http://jakarta.apache.org/cactus/misc/cactus-report.xsl
复制cactus-report.xsl到项目的根目录。
在测试结果显示时需要此样式。
[9] 验证目录结构
执行完以上操作, 目录结构如下图:
.
|-- src
| |-- LoginServlet.java
| `-- TestLoginServlet.java
`-- WebRoot
|-- cactus-report.xsl
|-- index.jsp
|-- META-INF
| `-- MANIFEST.MF
`-- WEB-INF
|-- cactus.properties---
|-- classes
| |-- LoginServlet.class
| `-- TestLoginServlet.class
|-- lib
| |-- cactus.core.framework.uberjar.javaEE.14-1.8.1.jar
| |-- cactus.integration.ant-1.8.1.jar
| |-- cactus.integration.shared.api-1.8.1.jar
| |-- commons-codec-1.4.jar
| |-- commons-httpclient-3.1.jar
| |-- commons-logging-1.1.jar
| |-- httpunit-1.6.jar
| `-- junit-3.8.2.jar
`-- web.xml
[10] 启动服务
[11] 执行测试
在浏览器地址栏,输入:
http://localhost:8080/ServletTestDemo/ServletTestRunner?suite=TestLoginServlet,回车,测试结果如下:
<?xml version="1.0" encoding="UTF-8" ?>
- <testsuite name="TestLoginServlet" tests="2" failures="0" errors="0" time="0.524">
<testcase name="testSaveSessionOk" time="0.517" />
<testcase name="testDoGet" time="0.006" />
</testsuite>
</testsuites>
此测试结果是以
xml
的形式显示。
如果结构以html报表的形式显示,可以输入如下地址:
http://10.0.3.133:8080/ServletTestDemo/ServletTestRunner?suite=TestLoginServlet&xsl=cactus-report.xsl
回车,结果如下:
Unit Test Results
Summary
Tests |
Failures |
Errors |
Success rate |
Time |
2 |
0 |
0 |
100.00% |
0.527 |
Note: failures are anticipated and checked for with assertions while errors are unanticipated. |
TestCase TestLoginServlet
Name |
Status |
Type |
Time(s) |
testSaveSessionOk |
Success |
|
0.516 |
testDoGet |
Success |
|
0.010 |
Back to top
[12]
|----------------------------------------------------------------------------------------|
版权声明 版权所有 @zhyiwww
引用请注明来源 http://www.blogjava.net/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2010-10-18 15:30
zhyiwww 阅读(1716)
评论(1) 编辑 收藏 所属分类:
软件测试