1.
为了使得项目的开发正规化,准备在3u的这个项目中使用专门的测试工具,经过多方面的比较,最后选择cactus,使用工具eclipse3.3+cactus+junit
还有就是这里的配置只是用于测试Servlet的,当然也可以用同样的方法配置其可以测试Filter,Jsp等,不过可能要换一个新一点的Cactus的包。
1.解压cactus安装文件到某一目录下
2.将lib下的jar拷贝到项目\web-inf\lib 下
3.创建cactus.properties文件,并加入classpath
(可选,在这里是为为了可以直接用eclipse的junit进行测试,所以这里还是加上吧)
4.我需要被测试的类
- package com.test.web;
-
- import java.io.FileNotFoundException;
- import java.io.IOException;
-
- import javax.servlet.RequestDispatcher;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
-
- import org.jdom.JDOMException;
-
-
-
-
-
- import com.jem.transdata.BsFormData;
- import com.jem.transdata.BsFormTable;
-
-
-
- public class LoginEdge extends HttpServlet {
-
- public boolean isValidUser(HttpServletRequest request) {
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- if (username == null || password == null || !username.equals("justin")
- || !password.equals("123456")) {
- return false;
- } else {
- return true;
- }
- }
- }
package com.test.web;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
// import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jdom.JDOMException;
// import com.ca.website.mainpage.MainPageSupport;
// import com.error.ErrorProcesser;
// import com.jem.orm.sql.BsSQLHandler;
// import com.jem.orm.sql.BsSqlXmlPaser;
import com.jem.transdata.BsFormData;
import com.jem.transdata.BsFormTable;
// import com.orm.util.SQLStatementIDS;
public class LoginEdge extends HttpServlet {
public boolean isValidUser(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username == null || password == null || !username.equals("justin")
|| !password.equals("123456")) {
return false;
} else {
return true;
}
}
}
6.我的测试类
- package com.test;
-
- import org.apache.cactus.ServletTestCase;
- import org.apache.cactus.WebRequest;
- import com.test.web.LoginEdge;
-
- public class LoginEdgeTest extends ServletTestCase {
-
- public void beginValidUser(WebRequest webRequest) {
- webRequest.addParameter("username", "justin");
- webRequest.addParameter("password", "123456");
- }
-
- public void testValidUser() {
- LoginEdge loginServlet = new LoginEdge();
- assertTrue(loginServlet.isValidUser(request));
- }
-
- public void beginInValidUser(WebRequest webRequest) {
- webRequest.addParameter("username", "guest");
- webRequest.addParameter("password", "guest");
- }
-
- public void testInValidUser() {
- LoginEdge loginServlet = new LoginEdge();
- assertFalse(loginServlet.isValidUser(request));
- }
-
- }
package com.test;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import com.test.web.LoginEdge;
public class LoginEdgeTest extends ServletTestCase {
public void beginValidUser(WebRequest webRequest) {
webRequest.addParameter("username", "justin");
webRequest.addParameter("password", "123456");
}
public void testValidUser() {
LoginEdge loginServlet = new LoginEdge();
assertTrue(loginServlet.isValidUser(request));
}
public void beginInValidUser(WebRequest webRequest) {
webRequest.addParameter("username", "guest");
webRequest.addParameter("password", "guest");
}
public void testInValidUser() {
LoginEdge loginServlet = new LoginEdge();
assertFalse(loginServlet.isValidUser(request));
}
}
7.在web.xml需要如下配置
- xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns=""
- xmlns:xsi=""
- xsi:schemaLocation="
- <A href="/web-app_2_4.xsd" target=_blank>/web-app_2_4.xsdA>">
-
- <servlet>
- <servlet-name>ServletRedirectorservlet-name>
- <servlet-class>org.apache.cactus.server.ServletTestRedirectorservlet-class>
- servlet>
- <servlet>
- <servlet-name>ServletTestRunnerservlet-name>
- <servlet-class>org.apache.cactus.server.runner.ServletTestRunnerservlet-class>
- servlet>
-
- <servlet-mapping>
- <servlet-name>ServletRedirectorservlet-name>
- <url-pattern>/ServletRedirectorurl-pattern>
- servlet-mapping>
- <servlet-mapping>
- <servlet-name>ServletTestRunnerservlet-name>
- <url-pattern>/ServletTestRunnerurl-pattern>
- servlet-mapping>
-
- web-app>
ServletRedirector
org.apache.cactus.server.ServletTestRedirector
ServletTestRunner
org.apache.cactus.server.runner.ServletTestRunner
ServletRedirector
/ServletRedirector
ServletTestRunner
/ServletTestRunner
8.在cactus.properties需要如下配置
cactus.contextURL =
cactus.servletRedirectorName =
ServletRedirector
cactus.enableLogging=true
9开始测试
输入以下
//
这里应该是把测试类当作一个suite传给TestRunner来运行,这跟平时用JUnit是一样的。
将出现测试结果:
- xml version="1.0" encoding="UTF-8" ?>
- - <testsuites>
- - <testsuite name="com.test.LoginEdgeTest" tests="2" failures="0" errors="0" time="0.625">
- <testcase name="testValidUser" time="0.5" />
- <testcase name="testInValidUser" time="0.016" />
- testsuite>
- testsuites>
阅读(1319) | 评论(0) | 转发(0) |