Cactus借助Jetty测试Servlet
这是一个WebProject,但不需要web.xml,因为用不到它
首先是待测试的LoginServlet.java
[java]
package com.jadyer.servlet;
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 = 6655227641354075528L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<table><tr><td>username</td><td>password</td></tr></table>");
out.flush();
out.close();
}
public boolean login(HttpServletRequest request){
String loginUser = (String)request.getSession().getAttribute("loginUser");
if(null==loginUser || !"http://blog.csdn.net/jadyer".equals(loginUser)){
System.out.println("用户[" + loginUser + "]登录失败");
return false;
}
System.out.println("用户[" + loginUser + "]登录成功");
return true;
}
}
package com.jadyer.servlet;
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 = 6655227641354075528L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<table><tr><td>username</td><td>password</td></tr></table>");
out.flush();
out.close();
}
public boolean login(HttpServletRequest request){
String loginUser = (String)request.getSession().getAttribute("loginUser");
if(null==loginUser || !"http://blog.csdn.net/jadyer".equals(loginUser)){
System.out.println("用户[" + loginUser + "]登录失败");
return false;
}
System.out.println("用户[" + loginUser + "]登录成功");
return true;
}
}
下面是Cactus编写的位于test SourceFolder下的测试用例LoginServletTest.java
其实它和上一篇文章"Cactus入门"里面的测试用例是一样的
[java]
package com.jadyer.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import junit.framework.Assert;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.xml.sax.SAXException;
import com.meterware.httpunit.WebResponse;
public class LoginServletTest extends ServletTestCase {
private LoginServlet loginServlet;
//它是在ServerSide执行的
public void setUp(){
loginServlet = new LoginServlet();
}
//beginXxx()方法是在ClientSide执行的
//如果想在testLogin()中获取到这里request添加的username参数,那么这个方法就应该命名为beginLogin()
public void beginLogin(WebRequest request){
request.addParameter("username", "Jadyer");
}
//它是在ServerSide执行的
public void testLogin(){
//获取GET参数
Assert.assertEquals(request.getParameter("username"), "Jadyer");
//登录失败
//session.setAttribute("loginUser", "玄玉<http://blog.csdn.net/jadyer>");
Assert.assertFalse(loginServlet.login(request));
//登录成功
session.setAttribute("loginUser", "http://blog.csdn.net/jadyer");
Assert.assertTrue(loginServlet.login(request));
}
//它是在ServerSide执行的
public void testDoGet() throws ServletException, IOException {
loginServlet.doGet(request, response);
}
//endXxx()方法是在ClientSide执行的,该方法对应testDoGet()
public void endDoGet(WebResponse resp) throws SAXException {
//这里使用com.meterware.httpunit.WebResponse,而非org.apache.cactus.WebResponse,前者提供了很多增强功能
//但在使用httpunit中的WebResponse时,要额外借助Tidy.jar作为辅助包
Assert.assertEquals(resp.getTables()[0].getCellAsText(0,0), "username");
Assert.assertEquals(resp.getTables()[0].getCellAsText(0,1), "password");
}
}
package com.jadyer.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import junit.framework.Assert;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.xml.sax.SAXException;
import com.meterware.httpunit.WebResponse;
public class LoginServletTest extends ServletTestCase {
private LoginServlet loginServlet;
//它是在ServerSide执行的
public void setUp(){
 
补充:软件开发 , Java ,